Java Reference
In-Depth Information
Example 18−3: Query.java (continued)
// Finally, end the HTML output
out.println("</body>");
}
}
Running the Query Servlet
To run the Query servlet yourself, you have to edit the WEB-INF/web.xml file to set
values for the various initialization parameters that tell it what JDBC driver to use,
what database to connect to, and so on. Your servlet container must also be able
to find the JDBC driver classes. You should be able to ensure this by placing a JAR
file of driver classes in the WEB-INF/lib/ directory of the web application, but,
unfortunately, Tomcat 3.1 cannot find database driver classes in the lib/ directory.
(It can find servlet classes stored there, however, so this bug probably has some-
thing to do with the way the database driver is loaded using Class.forName() .) In
any case, the solution is to place your JDBC driver JAR file in your CLASSPATH
before starting Tomcat.
A Login Screen with JSP
Take another quick look at the doGet() method shown in Example 18-3; it con-
tains a number of println() method calls that output HTML tags. One of the
problems with servlets of this type is that they include hardcoded HTML tags
locked inside Java classes where web designers and graphic artists cannot get at
them. JavaServer Pages, or JSP, is an attempt to correct this situation. Instead of
embedding HTML tags within Java code, JSP pages embed Java code within HTML
pages.
Example 18-4 is a listing of the JSP page login.jsp . This page displays the login
screen pictured in Figure 18-3 and performs trivial (and insecure) password verifi-
cation before redirecting the user's browser to some other page (specified via a
request parameter).
The first thing you'll notice about this JSP example is that it contains a lot of
HTML-style tags that begin with <% and end with %> . These are JSP tags. The fol-
lowing table summarizes the types of JSP tags and their purposes: *
Ta g
Purpose
<%--...--%>
A JSP comment. Unlike HTML com-
ments, JSP comments are stripped dur-
ing the JSP compilation process and
never appear in the output page.
* Note that I make no attempt here to document the complete JSP syntax. Consult a JSP reference, such
as the O'Reilly's soon-to-be-published JavaServer Pages , for that purpose. You can also find documen-
tation and tutorial information on Sun's web site ( http://java.sun.com/pr oducts/jsp ). One particularly
useful item is a “JSP Syntax Reference Card” at http://java.sun.com/pr oducts/jsp/pdf/card11a.pdf .
Search WWH ::




Custom Search