Recipe 5.5 Creating a JSP from Scratch as a JSP Document


Problem

You want to create a JSP document in an XML editor's tool.

Solution

Open up your XML editor of choice and create the JSP using only XML elements.

Discussion

A JSP document is a namespace-aware, well- formed XML file that contains JSP standard actions (such as jsp:include and jsp:useBean ), custom actions (such as JSTL custom tags), and the XML equivalents of JSP directives. Table 5-1 specifies the XML equivalents for common JSP directives. Write the JSP document in an XML editor, preferably one where you can check its well- formedness . The JSP document has to be a well-formed XML document to be eligible for placement into a JSP container and execution.

Table 5-1. XML equivalents for JSP directives

Directive

Example

JSP document equivalent

<page>

<%@ page import="java.util.Date" %>

<jsp:directive.page import="java.util.Date" />

<include>

<%@ include file="footer.html" %>

<jsp:directive.include file="footer.html" />

<taglib>

<%@ taglib uri="WEB-INF/tlds/xml_gen.tld" prefix="t" %>

<jsp:root jsp:id="0" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0" xmlns:t="urn:jsptld:/WEB-INF/tlds/xml_gen.tld">

In JSP 1.2, the only way to identify a JSP page as XML is by having a jsp:root element as the root. However, JSP 2.0 offers several new options ”the JSP 2.0 specification states that a JSP document can also be distinguished from a JSP in non-XML syntax by a jsp-property- group element in the deployment descriptor, a .jspx file extension, or a jsp:root root element.


This recipe shows a simple JSP page and its XML equivalent, then repeats the comparison with the addition of a custom tag and a runtime expression for a JSP element attribute. Example 5-9 is a simple file in JSP page syntax showing the web server's local time.

Example 5-9. A simple JSP page-syntax file
 <%@page contentType="text/html"%> <%@page import="java.util.Date"%> <html>   <head><title>Welcome to the Web</title></head>   <body>     <h2>Welcome to the Web</h2>     The server's local time is <%=new Date( ) %>.   </body> </html> 

This JSP has two page directives and a JSP expression that displays a date and time string on the browser page. Figure 5-1 shows the execution of this page in a browser.

Figure 5-1. Simple JSP before XML conversion
figs/jsjc_0501.gif

This page can be converted into a JSP document by cutting and pasting the code into an XML editor and replacing non-XML constructs with XML elements. Example 5-10 is the JSP document equivalent of Example 5-9.

Example 5-10. A simple JSP document as well-formed XML
 <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">   <jsp:directive.page contentType="text/html"/>   <jsp:directive.page import="java.util.Date"/>   <html>     <head><title>Welcome to the Web</title></head>     <body>       <h2>Welcome to the Web</h2>       The server's local time is <jsp:expression>new Date( )</jsp:expression>.     </body>   </html> </jsp:root> 

Example 5-10 has jsp:directive.page elements instead of traditional JSP directives, which are not valid XML elements because of the <%@ syntax. Anything in a JSP page that uses <% -style delimiters cannot be used to distinguish JSP elements, because then the JSP document will not pass an XML well-formedness test.

Example 5-11 is a more complex JSP page with a taglib directive that specifies the core tag library from JSTL 1.0; the page also uses Expression Language (EL) code. Further, the page has a jsp:useBean element that sets a java.util.Date variable dateString to page scope.

Example 5-11. A JSP page presenting a complex XML conversion
 <%@page contentType="text/html"%> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html>   <head><title>Welcome to the Web</title></head>   <body>     <h2>Welcome to the Web</h2>     Hello, <c:out value="${param.firstName} ${param.lastName}"/><br><br>     <jsp:useBean id="dateString" class="java.util.Date"/>     The time is <c:out value="${dateString}" />.<br><br>     The value of 10 + 24 + 35 = <c:out value="${10 + 24 + 35}" />    </body> </html> 

Example 5-12 is the same JSP page converted to a JSP document.

Example 5-12. Referring to tag libraries ( taglibs ) in a JSP document
 <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"          xmlns:c="http://java.sun.com/jstl/core" version="2.0">   <jsp:directive.page contentType="text/html"/>   <html>     <head><title>Welcome to the Web</title></head>     <body>       <h2>Welcome to the Web</h2>       <jsp:text>Hello </jsp:text>       <c:out value="${param.firstName} ${param.lastName}"/><br></br><br></br>       <jsp:useBean id="dateString" class="java.util.Date"/>       <jsp:text>The time is </jsp:text><c:out value="${dateString}" />.               <br></br><br></br>       <jsp:text>The value of 10 + 24 + 35 = </jsp:text>       <c:out value="${10 + 24 + 35}" />      </body>   </html> </jsp:root> 

In a JSP document, any tag libraries can be included as namespace attributes, such as in the jsp:root element, as shown here:

 <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"          xmlns:c="http://java.sun.com/jstl/core" version="2.0"> 

The jsp:text element can be used to contain any template data in the JSP document. You can use the JSP standard actions such as jsp:useBean and custom tags like c:out with the same syntax used in a JSP page.

Figure 5-2 shows the browser output of the JSP document in Example 5-12. This page was requested by using this URL: http://localhost:8080/home/example_xml2.jsp?firstName=Bruce&lastName=Perry .

Figure 5-2. Output from Example 5-11
figs/jsjc_0502.gif

Here is what the HTML source code looks like, if you chose "View Source" from the browser menu (with some carriage returns added for readability):

 <html><head><title>Welcome to the Web</title></head> <body> <h2>Welcome to the Web</h2> Hello Bruce Perry<br/><br/> The time is Mon Feb 10 16:20:05 EST 2003.<br/><br/> The value of 10 + 24 + 35 = 69 </body></html> 

See Also

Recipe 5.6 on generating an XML view from a JSP; Chapter JSP.6 (JSP documents) of the JSP 2.0 specification; Chapter JSP.10 ( X ML views) of the JSP 2.0 specification.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net