[ Team LiB ] Previous Section Next Section

20.5 Hello JSP

We now transition from servlet examples to JSP examples. One of the shortcomings of any servlet that produces nontrivial output is that they often contain embedded HTML tags locked up within Java classes where web designers cannot get to them. JSP is designed to improve this situation: instead of embedding HTML within Java code, it embeds Java code within HTML pages. (And, as we'll see, JSP 2.0 and the Java Standard Tag Libraries dramatically reduce the amount of Java code that must be intermixed with HTML.)

Example 20-4 is a simple JSP page that performs a function similar to the HelloNet servlet of Example 20-1. If invoked with a parameter named "name", it simply greets the named user. If there is no parameter, it displays an HTML form to request the user's name and then greets the user when the form is submitted.

Example 20-4 uses JSP 1.x, and the main thing to note about the example is that it consists of HTML code intermingled with Java code which is contained within pseudotags. Remember that JSP pages are compiled into servlet classes. <% and %> delimit blocks of Java code, which are inserted literally into the body of the servlet's doGet( ) or doPost( ) method. Note that these "scriptlet" tags can contain code fragments that are not complete statements. A fragment may end in the middle of an if block, to be followed by HTML tags (which are inserted literally into the servlet's output) and then followed by another <%...%> scriptlet that closes the if block.

The tag <%=...%> is like a scriptlet, but contains a single Java expression (instead of arbitrary statements) whose value is placed in the servlet's output at that point. Note also that the example begins with a <%@page...%> directive, which declares that the JSP page outputs an HTML document. Table 20-1 summarizes the syntax of JSP 1.x pages.

Table 20-1. JSP 1.x syntax

Tag

Purpose

<%- -...- -%>

A JSP comment. Unlike HTML comments, JSP comments are stripped during the JSP compilation process and never appear in the output page.

<@page...%>

The page directive. Every JSP page must have one. Common attributes specify the page language, the content type of the output, the page buffer size, and the list of packages to import.

<%@include file="URL"%>

Includes the specified file at compilation time.

<%@taglib uri="taglibId"prefix="tagPrefix"%>

Declares a tag library for the page. The uri attribute uniquely identifies the library, and the prefix attribute specifies the prefix by which it is known on the page.

<%!...%>

A declaration tag. These delimiters surround Java code that becomes methods and fields of the resulting servlet class.

<%=...%>

An expression tag. Contains a Java expression. The tag is replaced with the expression value at runtime.

<%...%>

A scriptlet. These delimiters surround Java code that becomes part of the _jspService( ) method, which is the JSP version of goGet( ) and doPost( ).

Example 20-4. hello.jsp
<%@page language='java' contentType='text/html'%>
<% // Begin scriptlet: this is Java code 
if (request.getParameter("name") == null) {  // If no name, request one
// Note that the scriptlet ends in the middle of a block.
%>
<%-- Now we switch to HTML tags and java expressions --%>
<form>
<i>Please enter your name: </i><input name="name">
<input type="Submit">
</form>
<%
// Back to Java code
} else {  // Otherwise, if there was a name parameter, greet the user
%>
Hello <%= request.getParameter("name") %>
<% } // end the Java if/else statement %>

20.5.1 Displaying a JSP Page

If you use the prepackaged je3.war file for the examples in this chapter, you can try out this example using a URL like this:

http://localhost:8080/je3/hello.jsp

Servlet containers automatically compile JSP pages as needed (and recompile them when they change, which is very useful during development). You can precompile a JSP page to avoid the compilation overhead on a production web server, but doing that is not covered in this chapter.

    [ Team LiB ] Previous Section Next Section