Introduction to Java Server Pages

From Wiki

Jump to: navigation, search
This article is a stub. You can help us by expanding it.

Contents

Overview of JSP Documents

A JSP document is a JSP page that is a namespace-aware XML document and that is identified as a JSP document by the JSP container. A JSP document can be identified as such in three ways:

  1. If there is a property group in the web.xml configuration file specifying, through an <is-xml> element, that a given file is a JSP document, then that indication overrides any other determination. Otherwise,
  2. If the file extension is .jspx, then the file is a JSP document. Otherwise,
  3. If the file is explicitly (by the file extension .jsp) or implicitly identified as a JSP page and the top element is a jsp:root element then the file is identified as a JSP document. This behavior provides backwards compatibility

with JSP 1.2.

Notice that there will be a translation-time error for a file that is identified as a JSP document but which is not a well-formed namespace-aware XML document.

JSP documents can be used in a number of ways, including:

  • JSP documents can be passed directly to the JSP container; this is becoming more important as more and more content is authored as XML, be it in an XML-based languages like XHTML or SVG, or for the exchange of documents in applications like Web Services. The generated content may be sent directly to a client, or it may be part of some XML processing pipeline.
  • JSP documents can be manipulated by XML-aware tools.
  • A JSP document can be generated from a textual representation by applying an XML transformation, such as XSLT.

• A JSP document can be generated automatically, for example, by serializing some objects.

JSP Documents Syntax

A JSP document can use XML elements as template data; these elements may have qualified names (and thus be in a namespace), or be unqualified. The jsp:text element can be used to define some template data verbatim. Since a JSP document must be a valid XML document, there are some JSP elements that can't be used in a JSP document. The elements that can be used are:

Much of the standard JSP syntax is already XML-compliant, including all the standard actions. The directive elements are used to specify information about the page itself that remains the same between page requests, for example, the scripting language used in the page, whether session tracking is required, and the name of a page that should be used to report errors, if any.

JSP Documents Semantics

JSP pages generate a response stream of characters from template data and dynamic elements. Template data can be described explicitly through a jsp:text element, or implicitly through an XML fragment. Dynamic elements are EL expressions, scripting elements, standard actions or custom actions. Scripting elements are represented as XML elements with the exception of request-time attribute expressions, which are represented through special attribute syntax.

  1. The first step in processing a JSP document is to process it as an XML document, checking for well-formedness, processing entity resolution and, if applicable, performing validation. As part of the processing XML quoting will be performed, and JSP quoting will not be performed later. A JSP Document with a DOCTYPE declaration must be validated by the container in the translation phase. Validation errors must be handled the same way as any other translation phase errors. JSP 2.0 requires only DTD validation for JSP Documents; containers should not perform validation based on other types of schemas, such as XML schema.
  2. The JSP document is passed to the JSP container which will then interpret it as a JSP page. Processing a JSP document starts with identification of the nodes of the document. Then, all textual nodes that have only white space are dropped from the document; the only exception are nodes in a jsp:text element, which are kept verbatim. Template data is either passed directly to the response or it is mediated through (standard or custom) actions. Following the XML specification (and the XSLT specification), whitespace characters are #x20, #x9, #xD, or #xA. The container will add, in some conditions, an XML declaration to the output; the rules for this depend on the use of jsp:root and jsp:output elements.

JavaServer Pages Standard Tag Library

Core Tag Library

Making core tags available

Declare the namespace xmlns:c="http://java.sun.com/jsp/jstl/core" in the jsp:root element.

A typical header of a JSP document using the core tag library is:

<?xml version="1.0" encoding="UTF-8"?>
<jsp:root version="2.0" 
 xmlns:jsp="http://java.sun.com/JSP/Page" 
 xmlns:c="http://java.sun.com/jsp/jstl/core">
 <jsp:directive.page contentType="text/html; charset=UTF-8"/>
 <jsp:directive.page isELIgnored="false" />
<jsp:output doctype-root-element="html"
 doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
 doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />

General-Purpose Tags (c:out, c:set, c:catch )

c:out

evaluates an EL and outputs the result

 <c:out value="${form.question}" escapeXml="false" default="What is JSP?"/>

where

  • value - the expression to be evaluated,
  • escapeXml determines whether characters <,>,&,'," in the resulting string should be converted to their corresponding character entity code and
  • defaultExpr specifies the default value if expr evaluates to null.

c:set

Set the value of a variable:

<c:set var="rowExpr">
<tr>
<td> \${row.userID} </td>
<td> \${row.name} </td>
<td> \${row.email} </td>
</tr>
</c:set>

Set a bean property:

<c:set target="${reservation}" property="noOfDays" value="${noOfDays}"/>

c:remove

c:catch

Catches a java.lang.Throwable thrown by any of its nested actions.

Actions that are of central importance to a page should not be encapsulated in a c:catch, so their exceptions will propagate to an error page, whereas actions with secondary importance to the page should be wrapped in a c:catch, so they never cause the error page mechanism to be invoked.

The exception thrown is stored in the scoped variable identified by var, which always has page scope.

<c:catch var="error">
 <c:import var="body" url="${param.page}"/>
</c:catch>
<c:if test="${not empty error}">
 <c:set var="body">
  This channel failed to load. Sorry.
  <!-- Here's why: <c:out value="${error}"/> -->
 </c:set>
</c:if>


Flow Control Tags (c:if, c:choose, c:when, c:otherwise)

<c:if test="${!empty(companyLogo)}">
 <c:set var="companyUrl" value="${company.url}"/>
 <c:choose>
  <c:when test="${!empty(companyUrl)}">
    <a href="${companyUrl}">
     <img src="${companyLogo}" title="${companyName}" alt="logo"/>
   </a>
  </c:when>
  <c:otherwise>
   <img src="${companyLogo}" title="${companyName}" alt="logo"/>
  </c:otherwise>
 </c:choose>
</c:if>

Iterator Tags (c:forEach, c:forTokens )

<c:forEach var="i" begin="1" end="10" step="1">
 <li>${i}</li>
</c:forEach>
<c:forTokens var="item" items="Argentina,Brazil,Chile" delims=",">
 <c:out value="${item}"/><br/>
</c:forTokens>

JSTL Formatting Tags

XML Tag Library

References

  • Sun Microsystems official JSP specification home page: http://java.sun.com/products/jsp/
  • Hans Bergsten, Java Server Pages, O'Reilly, 3rd Edition, December 2003.
  • Java 2 enterprise Edition 1.4 Bible, Wiley Publ. 2003.
Personal tools