Home
TOC Index |
What Is a JSP Page?
A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format, such as HTML, SVG, WML, and XML; and JSP elements, which construct dynamic content. A syntax card and reference for the JSP elements are available at
http://java.sun.com/products/jsp/technical.html#syntaxThe Web page in Figure 13-1 is a form that allows you to select a locale and displays the date in a manner appropriate to the locale.
Figure 13-1 Localized Date Form
The source code for this example is in the
docs/tutorial/examples/web/date
directory created when you unzip the tutorial bundle. The JSP pageindex.jsp
used to create the form appears below; 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 contains the following types of JSP constructs:
- Directives (
<%@page ... %>
) import classes in thejava.util
package and theMyLocales
class, and set the content type returned by the page.- The
jsp:useBean
element creates an object containing a collection of locales and initializes a variable that points to that object.- Scriptlets (
<% ... %>
) retrieve the value of thelocale
request parameter, iterate over a collection of locale names, and conditionally insert HTML text into the output.- Expressions (
<%= ... %>
) insert the value of the locale name into the response.- The
jsp:include
element sends a request to another page (date.jsp
) and includes the response in the response from the calling page.<%@ page import="java.util.*,MyLocales" %> <%@ page contentType="text/html; charset=ISO-8859-5" %> <html>
<head><title>Localized Dates</title></head> <body bgcolor="white"><jsp:useBean id="locales" scope="application" class="MyLocales"/>
<form name="localeForm" action="index.jsp" method="post"> <b>Locale:</b> <select name=locale><% String selectedLocale = request.getParameter("locale"); Iterator i = locales.getLocaleNames().iterator(); while (i.hasNext()) { String locale = (String)i.next(); if (selectedLocale != null && selectedLocale.equals(locale)) { %>
<option selected><%=locale%></option> <% } else { %> <option><%=locale%>
</option> <% } } %> </select> <input type="submit" name="Submit" value="Get Date"> </form><jsp:include page="date.jsp"/>
</body> </html>To build, deploy, and execute this JSP page:
- In a terminal window, go to
docs/tutorial/examples/web/date.
- Run
ant
build
. Thebuild
target will spawn any necessary compilations and copy files to thedocs/tutorial/examples/web/date/build
directory.- Run
ant
install
. Theinstall
target notifies Tomcat that the new context is available.- Open the date URL
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.
Home
TOC Index |
This tutorial contains information on the 1.0 version of the Java Web Services Developer Pack.
All of the material in The Java Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.