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
1
vote
1 answer

How do I figure out why a tomcat6 webapps dir symbolic link works on one machine, but not another?

I recently built an Ubuntu virtual machine for MarkLogic development. The machine was then distributed to 4 other developers to provide a consistent dev platform. Now I'm setting up another piece to the dev puzzle that consists of some java…
Peter
  • 218
  • 2
  • 8
1
vote
1 answer

tomcat on windows + pages can not link to CSS

we have tomcat on a windows server that everyone on the devteam can access. on this machine the stylesheet/.js links work fine. i copied the war down to my local machine and installed apache on my local machine. Now i can connect to the site at…
Bdawg
  • 11
  • 2
1
vote
1 answer

Creating a configuration based virtual directory in WEB-INF

Within a web application I want to create a "virtual directory" that lives under the WEB-INF directory. In effect what I am trying to accomplish is the same effect as creating a soft link in the filesystem if the application were run exploded. I am…
M. Jessup
  • 121
  • 1
  • 4
1
vote
1 answer

Websphere 7: Getting 'Internal Server Error' when accessing JSPs. Servlets ok

I have just installed WS 7 on our stage server. The test server was done some months ago. I have deployed the same applications to stage as on test. One application is causing me problems. I call the login servlet but the result is an 'Internal…
paul
  • 181
  • 1
  • 7
1
vote
1 answer

Why does Tomcat try to use the cache when compilation failed?

For some reason, it appears Tomcat is trying to hit its compilation cache when compilation failed. For example, if I create a JSP containing nothing but Hello, <%=world%>!, predictably, I get an error: org.apache.jasper.JasperException: Unable to…
etheros
  • 294
  • 1
  • 8
0
votes
1 answer

Execute JSP files in IIS7 and Tomcat6

I want to execute JSP files in on Windows Server having IIS7, Tomcat6 and Java 1.6 installed. Already Installed: Java 1.6 IIS7 Tomcat6 BonCode Apache Tomcat AJP 1.3 Connector Current scenario: I am able to execute JSP files on the server if taken…
0
votes
1 answer

Can I deploy a war or java web application into Azure with git?

I allways use control version to develop webs and apps. Now we will make an java web app and deploy it in Microsoft Azure and I've seen that you can upload content with git. But in the case of java webs, I think that you need to upload the war and I…
PhoneixS
  • 536
  • 5
  • 7
0
votes
1 answer

.War updates for Jboss

There is a .war file running well in JBoss environment. I need to update some little logic in some of the .jsp files. The steps I followed was First I change the extension to .zip extract out a particular .jsp file Then I make some small changes…
0
votes
1 answer

Tomcat localhost page showing HTTP500 but I can access the 'sample' war / app but no others - Jenkins installed

I'm trying to set up my server properly so that I can run servlets on it with the help from Jenkins. Within the webapps folder are two war files. One for jenkins and one for the sample from apache. Both work fine but when trying to access…
null
  • 139
  • 2
  • 10
0
votes
1 answer

How to create new Servlet and JSP project on Ubuntu?

I've installed Tomcat 7, javaservlet 3.0 and openjdk 7. I read that I need to install Eclipse and Web Tool platform (from eclipse) but I can't find it. I install few packages from eclipse there is no dynamic web project that I need to create. I…
jcubic
  • 230
  • 1
  • 4
  • 14
0
votes
3 answers

Tomcat 7 Throwing Unsupported Class Version Error

I'm setting up a Tomcat server to host JSP sites. I have created a test application in Eclipse to make sure everything is working. All it does is print text in JSP, then call a function in a custom class which also prints text. This runs fine in…
Doug
  • 101
  • 1
  • 1
  • 3
0
votes
1 answer

JSP Content Issue in Tomcat

There is one application where I work where there are still manual builds used i.e manually moving the servlet classes and jsp files from Dev to QA and finally to Prod. This is the method used in this application which cant be changed for some wierd…
gautam vegeta
  • 115
  • 1
  • 6
0
votes
1 answer

plesk+ tomcat + mod_jk + jsp gives 404

I trying to install myhello-world.war to plesk tomcat5, I doing "install java webapp", but when i go http://domain.com/mytomcat-helloworld/pages/myhelloworld.js I…
zb'
  • 117
  • 7
0
votes
1 answer

What percent of time my app server spends rendering html

My employer asked me this and I'm trying to figure out ways to measure this. We have have several applications - some Java with Tomcat and some .NET under IIS. To make things a little easier on myself I'm only going go deal with the Java side for…
sproketboy
  • 123
  • 4
0
votes
2 answers

Tomcat thread consumes maximum CPU

We have a Tomcat 7.0.21 server running on OpenJDK Server VM (20.0-b11 mixed mode). The webapp runs well for usually several hours or days after startup - typical CPU load is 1-2%. At some point, the tomcat process starts to consume 100% CPU. At…
ricsearle
  • 1
  • 1
  • 2