[ Team LiB ] Previous Section Next Section

Recipe 1.2 Writing a JSP


You want to create a JSP and include it in a web application.


Create the JSP as a text file using HTML template text as needed. Store the JSP file at the top level of the web application.


A JavaServer Pages (JSP) component is a type of Java servlet that is designed to fulfill the role of a user interface for a Java web application. Web developers write JSPs as text files that combine HTML or XHTML code, XML elements, and embedded JSP actions and commands. JSPs were originally designed around the model of embedded server-side scripting tools such as Microsoft Corporation's ASP technology; however, JSPs have evolved to focus on XML elements, including custom-designed elements, or custom tags , as the principal method of generating dynamic web content.

JSP files typically have a .jsp extension, as in mypage.jsp. When a client requests the JSP page for the first time, or if the developer precompiles the JSP (see Chapter 5), the web container translates the textual document into a servlet.

The JSP 2.0 specification refers to the conversion of a JSP into a servlet as the translation phase. When the JSP (now a servlet class) responds to requests, the specification calls this stage the request phase. The resulting servlet instance is called the page implementation object.

A JSP compiler (such as Tomcat's Jasper component) automatically converts the text-based document into a servlet. The web container creates an instance of the servlet and makes the servlet available to handle requests. These tasks are transparent to the developer, who never has to handle the translated servlet source code (although they can examine the code to find out what's happening behind the scenes, which is always instructive).

The developer focuses on the JSP's dynamic behavior and which JSP elements or custom-designed tags she uses to generate the response. Developing the JSP as a text-based document rather than Java source code allows a professional designer to work on the graphics, HTML, or dynamic HTML, leaving the XML tags and dynamic content to programmers.

Example 1-2 shows a JSP that displays the current date and time. The example JSP shows how to import and use a custom tag library, which Chapter 23 describes in great detail. The code also uses the jsp:useBean standard action, a built-in XML element that you can use to create a new Java object for use in the JSP page. Here are the basic steps for writing a JSP:

  1. Open up a text editor, or a programmer's editor that offers JSP syntax highlighting.

  2. If you are developing a JSP for handling HTTP requests, then input the HTML code just as you would for an HTML file.

  3. Include any necessary JSP directives, such as the taglib directive in Example 1-2, at the top of the file. A directive begins with the <%@ s.

  4. Type in the standard actions or custom tags wherever they are needed.

  5. Save the file with a .jsp extension in the directory you have designated for JSPs. A typical location is the top-level directory of a web application that you are developing in your filesystem.

Some JSPs are developed as XML files, or JSP documents, consisting solely of well-formed XML elements and their attributes. The JSP 2.0 specification recommends that you give these files a .jspx extension. See Recipe 5.5 for further details on JSP documents.

Example 1-2. A JSP file that displays the date
<%-- use the 'taglib' directive to make the JSTL 1.0 core tags available; use the uri 
"http://java.sun.com/jsp/jstl/core" for JSTL 1.1 --%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<%-- use the 'jsp:useBean' standard action to create the Date object;  the object is set 
as an attribute in page scope 
<jsp:useBean id="date" class="java.util.Date" />

<head><title>First JSP</title></head>
<h2>Here is today's date</h2>

<c:out value="${date}" />


To view the output of this file in a browser, request the file by typing the URL into the browser location field, as in: http://localhost:8080/home/firstJ.jsp. The name of the file is firstJ.jsp. If this is the first time that anyone has requested the JSP, then you will notice a delay as the JSP container converts your text file into Java source code, then compiles the source into a servlet.

You can avoid delays by precompiling the JSP. If you request the JSP with a jsp_precompile=true parameter, Tomcat converts the JSP, but does not send back a response. An example is http://localhost:8080/home/firstJ.jsp?jsp_precompile=true.

Figure 1-3 shows the JSP output in a browser.

Figure 1-3. Output from the firstJ.jsp page

If you select "View Source" from the browser menu to view the page's source code, you won't see any of the special JSP syntax: the comment characters (<%-- --%> ), the taglib directive, the jsp:useBean action, or the c:out tag. The servlet sends only the template text and the generated date string to the client.

See Also

Recipe 5.1-Recipe 5.3 on precompiling JSPs; Chapter 2 on deploying servlets and JSPs; Recipe 1.1 and Recipe 1.3 on writing and compiling a servlet; Recipe 1.4 on packaging servlets and JSPs; Recipe 1.5 on creating the deployment descriptor; the J2EE tutorial from Sun Microsystems: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/J2eeTutorialTOC.html; Hans Bergsten's JavaServer Pages (O'Reilly).

    [ Team LiB ] Previous Section Next Section