Chapter 24: Creating JavaServer Pages
This section lists the most useful syntax elements available in the JavaServer Pages API with simple usage examples. For complete details, see the JavaServer Pages 1.2 specification, available. For a reference card that includes all the attributes of tags and directives, see the JavaServer Pages Syntax Card .
Directives are messages to the JSP engine that provide global information for the page or include a file of text or code. Directives begin with the character sequence <%@ followed by the name of the directive and one or more attribute definitions. They end with the character sequence %>.
There are three directives: page, include, and taglib.
The page directive defines attributes that apply to an entire JSP, including language, the class being extended, packages imported for the entire page, the size of the buffer, and the name of an error page. For example:
<%@ page language="java" import="mypkg.*" session="true" errorPage="ErrorPage.jsp" %>
For more information about error pages, see “Error handling”.
The include directive includes a static file, parsing the file’s JSP elements:
<%@ include file="header.htm" %>
The taglib directive defines the name of a tag library and its prefix for any custom tags used in a JSP:
<%@ taglib uri="http://www.mycorp/printtags" prefix="print" %>
If the tag library includes an element called doPrintPreview, this is the syntax for using that element later in the page:
For more information, see “Customized tag libraries”.
Scripting elements manipulate objects and perform computations. The character sequence that precedes a scripting element depends on the element’s type: <% for a scriptlet, <%= for an expression, and <%! for a declaration. Scriptlets, expressions, and declarations are all closed with the sequence %>.
Scriptlets contain a code fragment valid in the scripting language:
<% cart.processRequest(request); %>
Expressions contain an expression valid in the page scripting language:
Value="<%= request.getParameter("amount") %>"
A declaration declares variables or methods valid in the page scripting language (usually Java, but other languages can be defined in the page directive):
<%! Connection myconnection; String mystring; %>
There are two kinds of comments:
HTML comments optionally contain an expression, and are sent to the client and can be viewed in the page source:
<!-- Copyright (C) 2001 Acme Software -->
Hidden comments document the source file and are not sent to the client:
<%-- Add new module here --%>
Standard tags perform common actions. The useBean, getProperty, and setProperty tags are all used with JavaBeans components. The useBean id attribute is the name of the bean and corresponds to the name attribute for getProperty and setProperty.
The useBean tag locates or instantiates a JavaBeans component:
<jsp:useBean id="labelLink" scope="session" class="LinkBean.labelLink" />
The bean class and classes required by the bean class must be deployed under a JavaCode base that is available to the Web Application where the JSP is installed. See “Java classes” for more information.
The getProperty tag gets the value of a JavaBeans component property so that you can display it in a result page:
<jsp:getProperty name="labelLink" property="url" />
The setProperty tag sets a property value or values in a JavaBeans component:
<jsp:setProperty name="labelLink" property="url" value="<%= labelLink.getURL() %>"/>
The include tag includes a static file or sends a request to a dynamic file:
<jsp:include page="/jsp/datafiles/ListSort.jsp" />
The forward tag forwards a client request to an HTML file, JSP file, or servlet for processing:
<jsp:forward page="/jsp/datafiles/ListSort.jsp" />
The plugin tag downloads plug-in software to the Web browser to execute an applet or JavaBeans component:
<jsp:plugin type="applet" code="Calc.class"
|Copyright © 2005. Sybase Inc. All rights reserved.|