Questions tagged [jsp]

Based on Java Servlet technology, JSP (JavaServer Pages) allows the platform-independent development of dynamic web applications.

JSP (JavaServer Pages)

JSP is a Java view technology running on a server which allows you to write template text in client side languages like HTML, CSS, JavaScript and so on. JSP supports the so-called taglibs which are backed by pieces of Java code with which you can control the page flow and/or output dynamically (programmatically). A well known taglib is JSTL. JSP also supports Expression Language (EL), with syntax like ${} which can be used to access backend data (actually, the attributes which are available in page, request, session and application scopes), mostly in combination with taglibs.

Lifecycle

When a JSP is requested for the first time or when the webapp starts up, the servlet container will compile the JSP file into a class extending HttpServlet and use it during the webapp's lifetime. You can find the generated source code in the server's work directory. In, for example, Tomcat, it's the /work directory. On a JSP request, the servlet container will execute the compiled JSP class and send the generated output (usually just HTML/CSS/JS) through the webserver over the network to the client side, which in turn displays it in the browser.

Hello World

This example uses JSTL and EL to display the user IP and the today's date.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<jsp:useBean id="date" class="java.util.Date" />

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>JSP Hello World</title>
    </head>
    <body>
        <h1>Hello</h1>
        <p>Welcome, user from <c:out value="${pageContext.request.remoteAddr}" />
        <p>It's now <fmt:formatDate value="${date}" pattern="MM/dd/yyyy HH:mm" />
    </body>
</html>

Save it as /hello.jsp and open it by http://localhost:8080/contextname/hello.jsp.

If JSTL doesn't work (the JSTL tags are not parsed/executed and still there in generated HTML output when you right-click and View Source in browser), then probably your servlet container doesn't support it out of the box (like Tomcat). You can install it by just dropping jstl-1.2.jar in /WEB-INF/lib. If it still doesn't work, then refer JSTL info page for more detail.

Scriptlets

You can also inline raw Java code in a JSP file using scriptlets (those <% %> things).

<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>JSP Hello World</title>
    </head>
    <body>
        <h1>Hello</h1>
        <p>Welcome, user from <%= request.getRemoteAddr() %>
        <p>It's now <%= new SimpleDateFormat("MM/dd/yyyy HH:mm").format(new Date()) %>
    </body>
</html>

Its use is however as per the JSP coding conventions discouraged for other purposes than quick prototyping.

Data pre-loading and form post-processing

To pre-load data for display in a JSP and to post-process a form submit, you'd like to use a Servlet. For more detail, see Servlets tag info page.

JavaScript

It's important to realize that JSP runs in the webserver, producing HTML output, and that JavaScript is part of the HTML output that runs in the browser only. So JSP and JavaScript don't run in sync as you might expect from the coding. To let JavaScript "access" JSP variables, all you need to do is to let JSP/JSTL/EL print it as if it is a Javascript variable. This way any JavaScript function, once executed in the browser, can access it. The below example prints the server side session ID as a JavaScript variable using EL:

<script>var jsessionid = '${pageContext.session.id}';</script>

If you open this page in a browser and do a View Source, then you'll see something like:

<script>var jsessionid = '4C147636FF923CA7EA642F2E10DB95F1';</script>

(note that those single quotes were thus mandatory to represent a JavaScript string value!)

Then, to let JSP "access" JavaScript variables, you need to send the JavaScript variable back to the server using an HTTP request, since that's the only way to send data from the browser to a webserver. You could:

  • use the HTML DOM to manipulate a hidden input field and fill it with the data, and if necessary submit the form using form.submit() so that it's available by request.getParameter().
  • use window.location to do a "redirect" to a new URL with the JavaScript variable as a request parameter.
  • use XMLHttpRequest to send an asynchronous (ajax) request with the JS variable as a request parameter.
  • let JavaScript set it as a cookie so that it's available by request.getCookies() in subsequent requests.

Facelets

Since Java EE 6, JSP has been succeeded by Facelets as default view technology for the Java EE MVC framework JSF (JavaServer Faces). Since the Java EE 6 tutorial, JSP is not treated in detail any longer. You need to head back to the Java EE 5 tutorial if you want to learn JSP. See also http://stackoverflow.com/questions/4845032/wheres-the-official-jsp-tutorial.

Online resources

Frequently Asked Questions

Related tag info pages

45 questions
5
votes
2 answers

Create tomcat war file from static web dir

I created a simple web application that consists of a dir with html, css, js. No server code. For reasons complicated to explain, my administrator insists on turning it into a .war file, so that it can easily be deployed on tomcat. Again, the…
Jeroen Ooms
  • 2,187
  • 7
  • 32
  • 51
3
votes
2 answers

Caused by: java.net.SocketException: Software caused connection abort: socket write error

I running JSP on Oracle 11g, Weblogic 10.3.4. I have 2 managed server and a oracle admin server installed. I am encountering an error where intermittently the log file of the 2 managed server and admin server will show java.net.SocketException:…
jrishere
  • 31
  • 1
  • 1
  • 3
3
votes
2 answers

Tomcat 6: Access Control Exception?

I'm trying to setup a tomcat6 server, and I'm trying to match another setup someone else established. However, my deployment (default Ubuntu install) uses a policy.d/ directory structure, and the established server just uses a catalina.policy file.…
Stefan Kendall
  • 1,069
  • 3
  • 17
  • 33
2
votes
1 answer

IIS 7.5 and Tomcat Setup: Error 500.19

I am setting up a 64-bit IIS 7.5 / Tomcat 7.0.26 server and I have received the following errors when I navigate to http://localhost - Error Summary HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related…
OrangeGrover
  • 585
  • 3
  • 10
  • 24
2
votes
1 answer

Will JSP Modification remain after Tomcat restart?

A war file is deployed in Tomcat 6 on Solaris 10. antiResourceLocking is not set to true. A change is to be made to a JSP in the application folder(exploded from the war file). No restart is made. If Tomcat is restarted inadvertently, will the JSP…
Shock
  • 43
  • 3
2
votes
1 answer

Apache sends 400 instead of 404

I've got an odd problem. Apache is replying with a 400 error code instead of 404 when ever a client requests a non existing jsp page (html request are answered correctly with a 404). The only time Apache serves correctly with a 404 is when the…
user58052
  • 31
  • 3
2
votes
2 answers

JSP / Tomcat / Apache setup overview on Fedora Core

For someone with so much Java experience, boy do I feel clueless - thanks in advance for your help in my grocking the present (Feb, 2010) JSP environment. Here's what I am hoping to learn: Do I understand correctly that most people use Apache to…
Richard T
  • 1,130
  • 11
  • 26
1
vote
1 answer

Java multiple HttpSession IDs for the same user

I have some problem with sessions. When I try my project from local sources and refresh several times a JSP that contains the following code:

Session ID=<%=session.getId()%>

It always display the same ID. Alright. The problem is I have…
Ommadawn
  • 225
  • 2
  • 7
1
vote
1 answer

Unable to start Tomcat8 because of org.apache.catalina.core.JasperListener?

I cannot start Tomcat8 because of the Jasper Listener, When I start Tomcat8 I got this message: org.apache.catalina.startup.Catalina.load Catalina.start using conf/server.xml: Error at (30, 65) : org.apache.catalina.core.JasperListener …
heaprc
  • 163
  • 3
  • 12
1
vote
1 answer

Tomcat to hadle only JSP - Apache proxy not working as expected

I've been given some source code for a PHP project that includes a .jsp file. I am able to run it as-is on my XAMPP set up on my Mac at work but I'm struggling to achieve the same on my Ubuntu setup at home. What I need is be able to drop .jsp…
1
vote
0 answers

JSF stylesheets missing with nginx

I developed a simple JSF application, using Glassfish4 as the application server. Currently it has only one page with administration things. When I run this on localhost everything seems to be in place. But I'd like to put it on a remote server.…
Rothens
  • 111
  • 4
1
vote
1 answer

How do I ensure the Context Path is the same when accessing a web app via Apache (proxy pass - port 80) and tomcat (port 8080)?

I have written a web app in Java/J2EE and am deploying it using apache (ec2 micro instance) to forward requests from port 80 to port 8080 (same machine). My httpd.conf looks like the following: ServerName localhost
user150878
  • 113
  • 1
  • 5
1
vote
2 answers

Tomcat session managment - independent session in one host

I have simple jsp-site on tomcat. To work with session I use simple jsp session object. But I need some sessions isolation in my web directories e.q. mysite.com/dir1 mysite.com/dir2 I want to use independent sessions for each dir1 and dir2 It's…
triclosan
  • 11
  • 1
  • 5
1
vote
1 answer

Tomcat Not Accepting Username/Password for Manager GUI

Tomcat 7 on Arch Linux. Was working before but I screwed it up so this is now a fresh install (removed package, dependencies, cleaned cache, rebooted, reinstalled, restarted). I try to log in to the Manager GUI but it keeps prompting me for my…
Hugh Guiney
  • 245
  • 1
  • 8
  • 20
1
vote
3 answers

Jquery/Javascript not working when accessing webpage through system IP instead of localhost

I don't face this problem while working on localhost only when I access the page using the IP address of my system this happens and it only happens with IE (works on all other browsers). By the way I'm using Tomcat V6.0.0.29, IE8 I tried debugging…
pranky64
  • 121
  • 1
  • 3
1
2 3