Home
JavaServer Pages (JSPs)
JSPs provide a server-side scripting technology that enables Java code to be embedded within Web pages, so JSPs have the appearance of HTML or XML pages with embedded Java code. When the page is executed, the Java code can generate dynamic content to appear in the resulting Web page. JSPs are compiled at runtime into servlets that execute to generate the resulting HTML or XML. Subsequent calls to the same JSP simply execute the compiled servlet.
JSP scripting elements (some of which are shown in Table | 2) are used to control the page compilation process, create and access objects, define methods, and manage the flow of control.
Table 2-2
Element Meaning Directive Instructions that are processed by the JSP engine when the page is compiled to a servlet: <%@ | . | gt; or <jsp:directive.page | . | gt; Declaration Allows variables and methods to be declared: <%! | . | gt; or <jsp:declaration> | . | t;/jsp:declaration> Expression® Java expressions, which are evaluated, converted to a String and entered into the HTML: <%= | . | gt; or <jsp:expression | . | gt; Scriptlet Blocks of Java code embedded within a JSP: <% | . | gt; or <jsp:scriptlet> | . | t;/jsp:scriptlet> Use bean Retrieves an object from a particular scope or creates an object and puts it into a specified scope: <jsp:useBean | . | gt; Get property Calls a getter method on a bean, converts the result to a String, and places it in the output: <jsp:getProperty | . | gt; Set property Calls a setter method on a bean: <jsp:setProperty | . | gt; Include Includes content from another page or resource: <jsp:include | . | gt; Forward Forwards the request processing to another URL: <jsp:forward | . | gt; Examples of JSP scripting elements
The JSP scripting elements can be extended, using a technology known as tag extensions (or custom tags), to allow the developer to make up new tags and associate them with code that can carry out a wide range of tasks in Java. Tag extensions are grouped in tag libraries, which we discuss shortly.
Some of the standard JSP tags are only provided in an XML-compliant version, such as <jsp:useBean | . | gt;. Others are available in both traditional form (for example, <%= | . | gt; for JSP expressions) or XML-compliant form (for example, <jsp:expression | . | gt;). These XML-compliant versions have been introduced in order to allow JSPs to be validated using XML validators.
JSPs generate HTML output by default-the Multipurpose Internet Mail Extensions (MIME) type is text/html. It might be desirable to produce XML (text/xml) instead in some situations. For example, a developer might want to produce XML output, which can then be converted to HTML for Web browsers, Wireless Markup Language (WML) for wireless devices, or VoiceXML for systems with a voice interface. Servlets can also produce XML output in this way-the content type being returned is set using a method on the HttpServletResponse object.
The JSP 2.1 specification defines now annotations for dependency injection on JSP tag handlers and context listeners. Moreover, the Unified Expression Language (EL) got some key additions:
A pluggable API for resolving variable references into Java objects and for resolving the properties applied to these Java objects
Support for deferred expressions, which can be evaluated by a tag handler when needed
Support for Ivalue expression. An EL expression used as an Ivalue represents a reference to a data structure
ibm.com/redbooks