© Copyright Marcus Green 2007
6.3)Write a JSP Document (XML-based document) that uses the correct syntax.
The key word here is document as opposed to page, though sometimes the two terms are used interchangeably. Everywhere else we have been considering JSP pages which are not fully formed xml documents. To allow JSP documents to be manipulated by standard XML programs the tags need to be in standardised XML structure. Thus for example you might have a JSP page where you are using a Java XML parser to transform all of one tag occurrence to another.
When I first discovered the existence of XML equivalents of JSP tags I tried converting existing pages on the mistaken that “XML is good”. This was an unrewarding experience and I suspect that the XML tag equivalents are only used where there is a definite need to automatically parse or generate valid JSP pages. This is probably a reason to pay additional attention to this topic, as most JSP you come across will not use the XML format tags. Note that in earlier versions of JSP a document had to begin with the <jsp:root element but this is not true of JSP 2.
Throughout this book I have tried to include the XML equivalent for each of the tags, thus for the import page tag I had
<%@ page import=”java.util.*,java.io.*” %>
The xml equivalent tag is
<jsp:directive.page import="java.util.*"/>
The <jsp:expression tag is the equivalent of the <%= tag. When translated it generates Java code that has an output terminated by a semi-colon. It is very similar to generating a System.out.print(“somestring”); in traditional Java programming. The following two lines of code are functionally equivalent.
<%= request.getParameter("username") %>
<jsp:expression>request.getParameter("username")</jsp:expression>
The <jsp:scriptlet tag is the equivalent of the <% tag. Thus the following lines of code are equivalent.
<html>
<%
for(int i =0; i <2; i++){
out.print(i);
}
%>
<jsp:scriptlet>
<![CDATA[
for(int i =0; i < 2; i++){
out.print(i);
}
]]>
</jsp:scriptlet>Note the use of the <![CDATA[ tags which effectively “escapes” the following code. The reason for this is so that the less than symbol < does not get interpreted as the start of the closure of the <jsp:scriptlet tag
The servlet code generated for declarations is external to the service method and the tag can be used to create class fields and for the creation of methods.
<jsp:declaration>
public String amethod() {
return "amethod";
}
</jsp:declaration>Which is the equivalent of
<%!
public String amethod() {
return "amethod";
}
%>JSP directives, i.e. Those tags that start with <%@ characters can be created with XML syntax in the form <jsp:directive.directivetext. The following are examples of JSP directives with the page syntax followed by the document (XML) syntax.
<%@ include file="standac.jsp"%> <jsp:directive.include file="standac.jsp"/> <%@ page import="java.util.*" %>
<jsp:directive.page import="java.util.*" /> <%@ page errorPage="errorPage" %>
<jsp:directive.page errorPage = "errorPage.jsp"/> <%@ page isErrorPage="true" %>
<jsp:directive.page isErrorPage="true"/>
The <jsp:text tag is designed for outputting template text, so it can be considered the equivalent of plain text within a jsp page. According to the Sun J2EE tutorial at
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPX3.html
“If you use jsp:text, all white space is preserved. “
However in my tests white space is preserved without the <jsp:text tag. I suspect that this tag is mainly for the convenience of parsers to flag up what is template text.
The <!-- is the standard XML comment tag and is the equivalent of the JSP <%-- tag. Unfortunately it does not work quite correctly as a JSP document comment tag as there is a clash with the meaning of the same tag in standard HTML. The contents of the <!-- tag are sent to the output stream, and thus appear in the final HTML page. I'm not quite sure if this should be considered a JSP document tag, but I feel it is unlikely to come up as a topic in the exam.
Other sources
Writing JSPs in XML by Stephanie
Fesler
http://www.onjava.com/pub/a/onjava/2001/11/28/jsp_xml.html
Carl Trusiak of Javaranch on JSP xml tags
http://www.javaranch.com/newsletter/Feb2002/xmljsp.html
According to Sun
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPX3.html
More JSP best practices by Dustin Marx
http://www.javaworld.com/javaworld/jw-07-2003/jw-0725-morejsp.html
This objective according to Mikalai Zaikin
http://java.boot.by/wcd-guide/ch06s03.html