What Is a JSP Page?

What Is a JSP Page?

A JSP page is a text document that contains two types of text: static data, which can be expressed in any text-based format (such as HTML, SVG, WML, and XML), and JSP elements, which construct dynamic content.

The recommended file extension for the source file of a JSP page is .jsp. The page can be composed of a top file that includes other files that contain either a complete JSP page or a fragment of a JSP page. The recommended extension for the source file of a fragment of a JSP page is .jspf.

The JSP elements in a JSP page can be expressed in two syntaxesstandard and XMLthough any given file can use only one syntax. A JSP page in XML syntax is an XML document and can be manipulated by tools and APIs for XML documents. This chapter and Chapters 6 through 8 document only the standard syntax. The XML syntax is covered in Chapter 5.


The web page in Figure is a form that allows you to select a locale and displays the date in a manner appropriate to the locale.

Localized Date Form

The source code for this example is in the <INSTALL>/javaeetutorial5/examples/web/date/ directory. The JSP page, index.jsp, used to create the form appears in a moment; it is a typical mixture of static HTML markup and JSP elements. If you have developed web pages, you are probably familiar with the HTML document structure statements (<head>, <body>, and so on) and the HTML statements that create a form (<form>) and a menu (<select>).

The lines in bold in the example code contain the following types of JSP constructs:

  • A page directive (<[email protected] ... %>) sets the content type returned by the page.

  • Tag library directives (<[email protected] ... %>) import custom tag libraries.

  • jsp:useBean creates an object containing a collection of locales and initializes an identifier that points to that object.

  • JSP expression language expressions (${ }) retrieve the value of object properties. The values are used to set custom tag attribute values and create dynamic content.

  • Custom tags set a variable (c:set), iterate over a collection of locale names (c:forEach), and conditionally insert HTML text into the response (c:if, c:choose, c:when, c:otherwise).

  • jsp:setProperty sets the value of an object property.

  • A function (f:equals) tests the equality of an attribute and the current item of a collection. (Note: A built-in == operator is usually used to test equality.)

Here is the JSP page:

   <%@ page contentType="text/html; charset=UTF-8" %>
   <%@ taglib uri="http://java.sun.com/jsp/jstl/core"
        prefix="c" %>
   <%@ taglib uri="/functions" prefix="f" %>
   <head><title>Localized Dates</title></head>
   <body bgcolor="white">
   <jsp:useBean id="locales" scope="application"

   <form name="localeForm" action="index.jsp" method="post">
   <c:set var="selectedLocaleString" value="${param.locale}" />
   <c:set var="selectedFlag"
     value="${!empty selectedLocaleString}" />
   <select name=locale>
   <c:forEach var="localeString" items="${locales.localeNames}" >
     <c:when test="${selectedFlag}">
               localeString)}" >
             <option selected>${localeString}</option>
   <input type="submit" name="Submit" value="Get Date">

   <c:if test="${selectedFlag}" >
      <jsp:setProperty name="locales"
         value="${selectedLocaleString}" />
      <jsp:useBean id="date" class="mypkg.MyDate"/>
      <jsp:setProperty name="date" property="locale"
      <b>Date: </b>${date.date}

To deploy the date application with NetBeans 5.5, follow these steps:

Start the Application Server.

In NetBeans 5.5, select FileOpen Project.

In the Open Project dialog, navigate to:


Select the date folder.

Select the Open as Main Project checkbox.

Click Open Project Folder.

In the Projects tab, right-click the date project, and select Deploy Project.

To deploy the date application with the Ant tool, follow these steps:

In a terminal window, go to <INSTALL>/javaeetutorial5/examples/web/date/.

Run the command ant. This target will spawn any necessary compilations, copy files to the <INSTALL>/javaeetutorial5/examples/web/date/build/ directory, and create a WAR file.

Start the Application Server.

Run ant deploy.

To run the example, do the following:

Set the character encoding in your browser to UTF-8.

Open your browser to http://localhost:8080/date.

You will see a combo box whose entries are locales. Select a locale and click Get Date. You will see the date expressed in a manner appropriate for that locale.

Some of the characters might not display properly if you don't have the appropriate language files installed on your machine. Consult the user guide or online help for your operating system to determine how you can install these language files.

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows