Java Reference
In-Depth Information
The login servlet example in Listing 12-3 shows how easy it is to write and deploy Java server-side
applications using JDBC. There is also an even easier way — you can use Java Server Pages.
Java Server Pages provide a means of using Java code within an HTML page. Simply write the static
HTML parts of the page in the normal way, and embed the Java code inside special tags. The Java is
executed in the JSP engine, and the result is sent to the client as HTML.
Note
Java Server Pages are not limited to generating HTML. JSP technology is also a great
way of generating XML, as you will see in Part IV .
You can use these four main types of elements in constructing a Java Server Page:
 
Markup language elements, which, in the case of a Web page, are HTML
 
Scripting elements, which let you specify a block of Java code
 
JSP directives, which control the JSP structure and environment
 
Actions, which let you specify execute commands such as loading a parameter
A special type of tag identifies the JSP-specific elements so that they are not confused with markup
language tags. These tags take one of the two following forms:
 
<% %>
 
<jsp: />
Although Java Server Pages offer a lot of advantages over basic servlets, they actually build on servlet
technology. The JSP engine compiles the JSP to a servlet the first time the page is requested, though
there are various ways of forcing the compile to ensure that the first real user doesn't see any delay due
to the translation. The simplest way to do this, of course, is to call the JSP page yourself to force a
compile.
The easiest way to demonstrate the advantages of Java Server Pages is to rework the login example to
use JSP. Since the JSP engine can serve static HTML as easily as dynamic HTML, even the login form
can be turned into a JSP page. The login form shown in Listing 12-4 is basically the same form shown
in Listing 12-1 . All that is required to make it a JSP page is to save it where the JSP engine can find it
and name it LoginForm.jsp .
Listing 12-4: A login form using JSP
<html>
<head>
<title>Member Login</title></head>
<body bgcolor="#c0c0c0">
<form method="POST" action="ProcessLogin.jsp">
<table>
<tr>
<td colspan=2><h3>Please log in:</h3></td>
</tr>
<tr>
<td>Username: </td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr><td colspan=2></td></tr>
Search WWH ::




Custom Search