|
Download
FAQ History |
|
API
Search Feedback |
What Is a JSP Page?
A JSP page is a text 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.
The JSP elements in a JSP page can be expressed in two syntaxes: standard and XML, though an individual page can only use one syntax. A JSP page in XML syntax is an XML document and can be manipulated by tools and APIs for XML documents. The chapters in this tutorial that cover JSP technology currently document only the standard syntax. The XML syntax will be addressed in a future release of the tutorial. A syntax card and reference that summarizes both syntaxes is available at
Example
The Web page in Figure 16-1 is a form that allows you to select a locale and displays the date in a manner appropriate to the locale.
![]()
Figure 16-1 Localized Date Form
The source code for this example is in the
<INSTALL>/jwstutorial12/examples/web/date/directory. The JSP page,index.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 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:useBeancreates 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 value of an are used to set tag attribute values.- 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:setPropertysets 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).<%@page contentType="text/html; charset=UTF-8" %> <%@tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@tagliburi="/functions" prefix="f" %> <html> <head><title>Localized Dates</title></head> <body bgcolor="white"> <jsp:useBeanid="locales" scope="application" class="mypkg.MyLocales"/> <form name="localeForm" action="index.jsp" method="post"> <c:setvar="selectedLocaleString" value="${param.locale}" /> <c:setvar="selectedFlag" value="${!empty selectedLocaleString}" /> <b>Locale:</b> <select name=locale> <c:forEachvar="localeString" items="${locales.localeNames}" > <c:choose> <c:whentest="${selectedFlag}"> <c:choose> <c:whentest="${f:equals(selectedLocaleString, localeString)}" > <option selected>${localeString}</option> </c:when> <c:otherwise> <option>${localeString}</option> </c:otherwise> </c:choose> </c:when> <c:otherwise> <option>${localeString}</option> </c:otherwise> </c:choose> </c:forEach> </select> <input type="submit" name="Submit" value="Get Date"> </form> <c:iftest="${selectedFlag}" > <jsp:setPropertyname="locales" property="selectedLocaleString" value="${selectedLocaleString}" /> <jsp:useBeanid="date" class="mypkg.MyDate"/> <jsp:setPropertyname="date" property="locale" value="${locales.selectedLocale}"/> <b>Date: </b>${date.date}</c:if> </body> </html>A sample
date.waris provided in<INSTALL>/jwstutorial12/examples/web/provided-wars/. To build, package, deploy, and execute this example:
- In a terminal window, go to
<INSTALL>/jwstutorial12/examples/web/date/.- Run
antbuild. This target will spawn any necessary compilations and copy files to the<INSTALL>/jwstutorial12/examples/web/date/build/directory.- Start Tomcat.
- Run
antinstall. Theinstalltarget notifies Tomcat that the new context is available.- Set the character encoding in your browser to UTF-8.
- Open the URL
http://localhost:8080/datein 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.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.