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 syntaxes--standard and XML--though 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 14 through 16 document only the standard syntax. The XML syntax is covered in Chapter 13. A syntax card and reference that summarizes both syntaxes is available at



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

Localized Date Form

Figure 12-1 Localized Date Form

The source code for this example is in the <INSTALL>/j2eetutorial14/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:

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}

A sample date.war is provided in <INSTALL>/j2eetutorial14/examples/web/provided-wars/. To build this example, perform the following steps:

  1. In a terminal window, go to <INSTALL>/j2eetutorial14/examples/web/date/.
  2. Run asant build. This target will spawn any necessary compilations and copy files to the <INSTALL>/j2eetutorial14/examples/web/date/build/ directory.

To package and deploy the example using asant, follow these steps:

  1. Run asant create-war.
  2. Start the Application Server.
  3. Run asant deploy-war.

To learn how to configure the example, use deploytool to package and deploy it:

  1. Start the Application Server.
  2. Start deploytool.
  3. Create a web application called date by running the New Web Component wizard. Select FileRight ArrowNewRight ArrowWeb Component.
  4. In the New Web Component wizard:
    1. Select the Create New Stand-Alone WAR Module radio button.
    2. In the WAR File field, enter <INSTALL>/docs/tutorial/examples/web/date/date.war. The WAR Display Name field shows date.
    3. In the Context Root field, enter /date.
    4. Click Edit Contents.
    5. In the Edit Contents dialog box, navigate to <INSTALL>/j2eetutorial14/examples/web/date/build/. Select index.jsp, functions.tld, and the mypkg directory and click Add, then click OK.
    6. Click Next.
    7. Select the No Component radio button, then click Next.
    8. Click Finish.
  5. Select FileRight ArrowSave.
  6. Deploy the application.
    1. Select ToolsRight ArrowDeploy.
    2. In the Connection Settings frame, enter the user name and password you specified when you installed the Application Server.
    3. Click OK.
    4. A pop-up dialog box will display the results of the deployment. Click Close.

To run the example, perform these steps:

  1. Set the character encoding in your browser to UTF-8.
  2. Open the URL http://localhost:8080/date in a browser.

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.