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
0
votes
1 answer

JSP: Trying to configure Apache2 + Tomcat6 using mod_jk

I'm trying to configure Apache2 + Tomcat using the mod_jk on an Ubuntu 10.04. I installed everything and configured, but it always returns the source code of my JSP files. I've read around that the most possible problem is that Httpd isn't…
0
votes
1 answer

Are there any JSP File for manage PostgreSQL database?

1. I need jsp file to manage PostgreSQL database?! Anyone know JSP file for manage PostgreSQL database?! 2. How I can connect to remote PostgreSQL Server with Webmin?!
akoori
  • 17
  • 2
0
votes
1 answer

How can I redirect a JSP to use HTTPS

My web app is running, so that access to /admin/login.jsp happens over an unsecure HTTP connection. I do not want to change web.xml file. How can I change access to /admin/login.jsp from HTTP to HTTPS without editing the web.xml file? Is there a way…
0
votes
1 answer

How set owner of file cms.war for ftpuser and owner of cms folder for tomcat user?

I'm using Tomcat Server. I would like the owner of the file cms.war to be the ftp user the tomcat user to be the owner of the cms/ folder. When I uploaded cms.war it was automatically deployed in cms/ folder and when I deleted cms.war the cms/…
0
votes
3 answers

Can JSP/ JSF be hosted in shared hosting plans?

I want to use JSF with Facelets/JSP in my website. Can I get Java hosting in shared hosting plans ? I want to re-develop a business website for my client's company.
Rajat Gupta
  • 301
  • 4
  • 17
0
votes
1 answer

How to set up a web server to serve jsp files in Fedora 15?

I have installed Fedora 15 on my system. Installed Web server using the command sudo yum groupinstall "Web Server" Have it running successfully at port 80. The files are put in "/var/www/html". It is supporting html and php. i would like to have…
0
votes
1 answer

Error with hosting a jsp file in Apache Tomcat 7.0.8

I have little experience with hosting and administering jsp sites in Apache Tomcat 7.0.8. Today I tried to host a site Apache Tomcat 7.0.8 but its giving me error like following: Bad Gateway The proxy server received an invalid response from an…
Guru
  • 101
  • 1
0
votes
1 answer

How to host web application in apache tomcat

I created a small web application in Java using eclipse. Now I want to host it in apache in my local machine. How to do this? I want some simple instructions not very technical details. Thanks in advance.
user58859
  • 518
  • 3
  • 7
  • 17
0
votes
1 answer

Apache forward JSP pages to Tomcat?

I have an Apache Server, in which my website running. But there are some JSP pages in my website. I have to forward my JSP pages and servlet to Tomcat. How can I make that possible?
Arman
  • 103
  • 3
0
votes
1 answer

Is it possible to make apache also work for jsp pages?

I've used wampserver for serving PHP requests. Now I want to use jsp,is it possible for apache? Otherwise I have to install tomcat,is there a wampserver alike tool out there?
apache
  • 3,027
  • 6
  • 25
  • 25
0
votes
1 answer

Cache JSP ByteCode with Tomcat?

Is there any way of caching the bytecode for JSP-powered sites with Tomcat? I'm getting really fed up of Tomcat taking up all the CPU for 10 minutes while it compiles 4 different webapps every time I restart it. I'm already using Jikes to "speed up"…
Mahmoud Al-Qudsi
  • 509
  • 1
  • 6
  • 21
-1
votes
1 answer

What can be turned into a docker image?

Assume I have the the following VM: -Centos -JRE 1.8 -Tomcat -Jar dependencies -My WAR file + all the server configuration files Now can I turn this whole thing into one Docker image, excluding the Centos? So next time I deploy it as a container…
another q
  • 1
  • 1
-1
votes
1 answer

Apache Tomcat standalone or with nginx or apache?

I will be rolling out a fairly big project that will be running on Tomcat. I will have a linux VPS for it (Debian). i wanted to know if it is recommended to run it by itself or behind a httpserver such as apache or nginx.
-1
votes
1 answer

Load balancing multiple PHP sites and JSP sites using Apache Server

I've been trying for the last couple of days to get this small (i though) scenario working. Small drawing of my architecture.       request          |      ApacheLB          |     |---------|  Apache1   Apache2  Tomcat1   Tomcat2 So far i can…
Robert
  • 1
  • 1
-1
votes
1 answer

tomcat cannot write files into WEB-INF folder

I used cpanel+tomcat .the Project structure is : ROOT ----index.jsp ---- sample.txt ----- /WEB-INF/classes/pack2/sample2.txt tomcat Can write into sample.txt with permission 664 . but cannot write into…
1 2
3