background image

A Simple JSP Page Example

<< JavaServer Pages Technology | What Is a JSP Page? >>
<< JavaServer Pages Technology | What Is a JSP Page? >>

A Simple JSP Page Example

A Simple JSP Page Example
The web page in
Figure 5­1
is a form that allows you to select a locale and displays the date in a
manner appropriate to the locale.
The source code for this example is in the tut-install/javaeetutorial5/examples/web/date/
directory. The JSP page, index.jsp, 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 contain the following types of JSP constructs:
A page directive (<%@page ... %>) sets the content type returned by the page.
Tag library directives (<%@taglib ... %>) import custom tag libraries.
jsp:useBean
is a standard element that 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 (see
Chapter 8, "Custom Tags in JSP Pages"
) 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
is another standard element that sets the value of an object property.
A function (f:equals) tests the equality of an attribute and the current item of a collection.
(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" %>
FIGURE 5­1
Localized Date Form
What Is a JSP Page?
The Java EE 5 Tutorial · September 2007
134