Template content is not, strictly speaking, a specific element. Rather, template content is everything in your JSP that is not an actual JSP element. Recall our first HelloWorld.jsp example (A simple JSP ); everything on that page was template content. The JSP specification states that everything that is not an actual JSP element is template content and should be passed, unchanged, into the output stream. For example, consider the following variation of our Hello World example:
<jsp:directive.page import="java.util.Date"/> <HTML> <HEAD><TITLE>Hello World JSP Example w/Current Time</TITLE></HEAD> <BODY> Hello World. The local server time is <%= new Date() %>. </BODY> </HTML>
This is the same as an earlier example, except we included a JSP directive to import the
java.util.Date class so you don't have to fully qualify the package name when you
Date object. You might assume, therefore, that since we haven't changed
the template content and have only specified an import statement, the output from this JSP is
the same as we received previously. However, that assumption is incorrect.
The fact is that we did change the template content: The carriage return following the
page directive is new and becomes part of the template content. You can confirm
this by viewing the HTML source for the live demo.
Remember: Everything that is not part of a JSP element is template content.
You can place comments in a JSP in three ways:
- Write an HTML comment
- Write a JSP comment
- Embed a comment within a scripting element
With the first way, you write a standard HTML comment, following this format:
<!- I am an HTML comment -->
Of course, because an HTML comment is just another piece of template content to the JSP container, you can embed JSP expressions within it. For example, you can write the following:
<!-- The time is <%= new java.util.Date() %> --> <HTML> <HEAD><TITLE>Hello World JSP Example</TITLE></HEAD> <BODY> Hello World. </BODY> </HTML>
This results in the date being embedded within the HTML comment. Nothing would be apparent in a browser view, but you can see the comment by viewing the HTML source for the live demo.
You can write a comment a second way by using the JSP notation:
<%-- I am a comment --%>
This comment is known as a hidden comment because it is an actual JSP comment and does not appear in the generated servlet. The user never sees the comment and won't have any way to know of its existence. Repeating the Hello World example, but this time with a JSP comment, yields the following:
<%-- This is our oft-repeated Hello World example: --%> <HTML> <HEAD><TITLE>Hello World JSP Example</TITLE></HEAD> <BODY> Hello World. </BODY> </HTML>
Again, remember that everything that is not a JSP element is template content. The carriage return following the comment is not part of the JSP comment element; it is part of the template content and is, therefore, visible in the browser.
The third way to write a comment is to embed a language-specific comment within a scripting element. For example, you can write the following:
<jsp:directive.page import="java.util.*"/> <jsp:declaration> /* date is a new member variable, initialized when we are instantiated. It can be used however we want, such as: out.println("Instantiated at " + date); // display our "birth" date */ Date date = new Date(); </jsp:declaration> <HTML> <HEAD><TITLE>Hello World JSP Example</TITLE></HEAD> <BODY> Hello World.<BR> This page was loaded into memory at <%= date %>.<BR> The current time is <%= new Date() %> </BODY> </HTML>
This version has a comment embedded within a scripting element, the declaration. In fact, there
is also a nested comment: the
// comment within the
/* ... */ block comment.
The only type of nested comment allowed by the JSP specification is a scripting language comment,
and even then, only when the scripting language (for example, Java) permits nested comments.
<% is a magic token the JSP container understands as the beginning of a JSP
element. How do you put a
<% into the output stream? For example, if you want to
use JSP to write a tutorial on the subject of JSP, how do you write a page so that its output contains
the source for a JSP? Won't those JSP elements be processed as well?
The solution to this problem is called escaping and entails encoding the content in a
modified form. The following table illustrates the set of escaping rules that is part of the JSP
specification. It indicates that when you want a
<% to be passed along literally
instead of being processed, you write it as
<\% in the template content.
|Within this element type ...||If you want to write ...||You escape the text as ...|
The following example illustrates some uses of these escaping sequences and illustrates an important point regarding HTML:
<HTML> <HEAD><TITLE>Hello World JSP Escape Sequences</TITLE></HEAD> <!-- This page was run at <%= new java.util.Date() %>, according to <\%=new Date() %\> --> <BODY> <P>Hello World.<BR> The local server time is <%= new java.util.Date() %>.<BR> The time is computed using the JSP expression <I><%=new Date()%>.</I></P> <P>The escaping sequence in the comment uses the JSP defined escape sequence, but that won't work within the normal HTML content. Why not? Because HTML also has escaping rules.</P> <P>The most important HTML escaping rules are that '<' is encoded as <I>"&lt;"</I>, '>' is encoded as <I>"&gt;"</I>, and '&' is encoded as <I>"&amp;"</I>. </P> </BODY> </HTML>
As illustrated in the previous example, you must be aware of not only the rules for JSPs but also
the rules for the template content. In this case, you write HTML content, and HTML also has
escaping rules. These rules say that you cannot write
<\% within HTML, except
within a comment. Why not? HTML treats the
< as a special character; it marks
the beginning of an HTML tag. Accordingly, HTML requires that you encode certain special
characters, such as those listed in the following table.
|Desired character||HTML escape sequence|
Many other useful escape sequences for HTML exist. Part of the HTML V4.01 specification includes a complete list of the official HTML escape sequences, known in the HTML standard as character entity references.