Chapter 24 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.1 specification, availablehttp://java.sun.com/products/jsp/download.html . For a reference card that includes all the attributes of tags and directives, see the JavaServer Pages Syntax Card http://java.sun.com/products/jsp/syntax.pdf .
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 page, 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" %>
Include directive and include standard tag Note that the include directive parses the file's contents, while the include tag does not.
The taglib directive defines the name of a tag library and its prefix for any custom tags used in a JSP page:
<%@ 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:
<print:doPrintPreview>
...
</print>
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:
<!-- Copyright (C) 2001 Acme Software -->
<%-- 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:setProperty name="cart" property="" />
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"
codebase="/utils/applets" >
Copyright © 2000 Sybase, Inc. All rights reserved. |