19.1 Overview of JSP Document
JSP can be written in 2 formats:
· JSP Page - A JSP page written in an HTML format with JSP elements expressed as <% ... %> tags. This is the format which we have used so far in the tutorial.
· JSP Document - A JSP written in an XML format with JSP elements expressed as XML elements. A JSP document is JSP written in XML format and therefore must comply with the XML standard.
JSP elements included in the JSP document must comply with the XML syntax.
19.2 JSP Document Rules
There are some rules that a JSP document should follow.
· JSP document must be well formed which means that each start tag must have a corresponding end tag and that the document must have only one root element.
· A JSP Document should be saved with the .jspx extension.
· A JSP Document must have a root element called "root" with a "version" attribute like: <jsp:root version="2.1" xmlns:jsp="http://java.sun.com/JSP/Page">
· By default content type of JSP document is text/xml so to display the document as html, you need to change the content type to text/html
19.3 JSP Document Syntax
Similar to directives , actions , scripting tags we discussed in previous chapters ,JSP documents supports all these tags but for each element , equivalent XML tag is defined .
19.3.1 Declaration
In standard JSP page , declaration tag is used as <%! . . %> but in JSP document , declaration tag is expressed as <jsp:declaration> . . . </jsp:declaration>
19.3.2 Scriptlets
In standard JSP page , scriptlet tag is used as <% . . %> but in JSP document , scriptlets tag is expressed as <jsp:scriptlet> . . . </jsp:scriptlet>
19.3.3 Expressions
In standard JSP page , expression tag is used as <%= . . %> but in JSP document , expressions tag is expressed as <jsp:expression> . . . </jsp:expression>
19.3.4 Comments
In standard JSP page , comments are defined as <%-- . . --%> but in JSP document , expressions tag is expressed as <!-- . . . -->
19.3.5 Directives
· Include directive will be expressed as <jsp:directive.include .. /> instead of <%@include . . . %>
· Page Directive will be expressed as <jsp:directive.page .. /> instead of <%@page ... %>
· Taglib directive will be expressed as <jsp:directive.taglib .. /> instead of <%@taglib ... %>
19.3.6 EL Operators
Operators need to be used as below –
Ø < will be written as lt
Ø >will be written as gt
Ø <= will be written as Le
Ø >= will be written as Ge
Ø != will be written as Ne
19.4 Hello World Example
Lets create a HelloWorld.jspx JSP document which will print “hello world” on screen
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"> <html> <head> <title> Hello World JSP Document </title> </head> <body> <h4> Hello World JSP Document </h4> </body> </html> </jsp:root>
Access HelloWorld.jspx document using http://localhost:8080/jsp-tutorial/HelloWorld.jspx
Since we did not change the content type of JSP document , the whole content is displayed as xml. Now change the content type (highlighted below )
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"> <html> <head> <title> Hello World JSP Document </title> </head> <jsp:directive.page contentType="text/html"/> <body> <h4> Hello World JSP Document </h4> </body> </html> </jsp:root>
19.5 JSP Scripting Tag Example
Lets write an example (ScriptingExample.jspx) to use scripting tags (expression, declaration and scriplet ) in JSP document
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"> <jsp:directive.page contentType="text/html"/> <html> <head> <title> JSP Document using Scripting </title> </head> <body> <jsp:declaration> String description = "description variable is a instance variable." ; public int sum(int argument1, int argument2) { int result = argument1+ argument2; return result; } </jsp:declaration> <jsp:scriptlet> <![CDATA[ out.println(description); out.println("<br/>"); int result = sum( 2, 3); ]]> </jsp:scriptlet> <jsp:expression> "Result of Sum :: " +result </jsp:expression> </body> </html> </jsp:root>
Access ScriptingExample.jspx document using http://localhost:8080/jsp-tutorial/ScriptingExample.jspx
19.6 JSP Directive Tag Example
Lets write an example (DirectiveExample.jspx) to use directive tags (page and include) in JSP document
a)Footer.jsp
<html> <head> <title> Footer</title> </head> <body> <h5> Footer Page </h5> </body> </html> b)DirectiveExample.jspx <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"> <jsp:directive.page contentType="text/html"/> <jsp:directive.page import="java.util.Enumeration"/> <html> <head> <title> JSP Document using Scripting </title> </head> <body> <jsp:scriptlet> <![CDATA[ Enumeration<String> headerEnumeration= request.getHeaderNames(); StringBuffer buffer = new StringBuffer(); while(headerEnumeration.hasMoreElements()) { String headerName= headerEnumeration.nextElement(); String headerValue =request.getHeader(headerName); buffer.append(headerName + ":" + headerValue); buffer.append("<br/>"); } ]]> </jsp:scriptlet> <jsp:expression> buffer.toString() </jsp:expression> </body> </html> <jsp:directive.include file="footer.jsp"/> </jsp:root>
Access DirectiveExample.jspx document using http://localhost:8080/jsp-tutorial/DirectiveExample.jspx
19.7 JSP Actions
JSP Actions are used the way they are used in JSP page. For more details refer Chapter 9- JSP Actions of this tutorial.