Appendix B. JSPs and Servlets
This appendix is meant to be a kind of bridge from your skills as a
system administrator, which probably include a familiarity with a
variety of scripting languages, to give you some familiarity with the
Java development side of Tomcat. Since Tomcat specializes in running
servlets and JSPs, we discuss what these are in a bit more detail.
First, though, we review a variety of network software architectures,
so you have a better idea of where servlets and JSPs fit into the big
picture of network software development.
Here are four general Java-centric approaches to networked
application development. Since almost all web applications need
access to a backend database, we also say a few words about how the
Java code accesses the database.
- Web application displays an applet, and the applet uses JDBC to connect to a database
�Slow; browser incompatibilities
- Download application using Java Web Start; application uses JDBC
�Avoids browser problem; performance can be slow due to lack of
caching
- Web application sends pure HTML and uses servlets and JSPs
�Servlets use JDBC; JSPs, if necessary, use the Java Standard
Template Library (JSTL)
�Closest to use of mod_perl, PHP, or CGI
�Good general approach
- Web application uses servlets, and servlets use EJBs to access database
�Claimed to be a good
"enterprise"-scale approach
(J2EE-style)
Briefly,
a servlet is a Java program designed to run in a servlet container
(we hope you didn't catch that circular definition),
and a JSP is a web page that can call Java code at request time. If
you're a system administrator or webmaster, you can
think of JSPs as just another scripting and templating language for
HTML pages; you can learn to write JSPs that call Java objects much
as you might have used objects in JavaScript. The difference is that
the Java runs on the server side, before the web page is sent to the
browser. It's more like PHP, or even ASP. Writing
Java classes such as servlets and JSP custom tags, however, is a task
probably best left to people trained in Java programming.
More precisely, a servlet is a Java program that uses the
javax.servlet package, subclasses either the
javax.servlet.http.HttpServlet or
javax.servlet.GenericServlet Java class, performs
some processing (anything the programmer wants that the servlet
container allows) in response to user input (such as clicking on a
link or filling in and submitting a web form), and generates some
kind of output that might be useful on the Web. A servlet can, of
course, generate an HTML page, but servlets can and have been written
to generate graphs and charts in GIF, PNG, and JPEG formats; printed
documents in PDF; or any format the developer can program.
We've said it before and we'll say
it again: you do not need to be a Java programmer to use Tomcat, or
even to use servlets. There are servlets available that you can use
without writing any code.
But JSP is one of the neatest features of Tomcat, so we wanted to say
a few words about it. A JavaServer Page is an HTML page that can call
Java language functionality, as well as functionality of other
languages if the JSP implementation integrates with them (Tomcat
4's JSP doesn't). Again, you
don't have to write any Java code to write a JSP,
although many JSPs that you will find today have some Java code
embedded in them. The design goal of JSPs is to remove
"raw" Java code from the web page
markup and to have the Java code isolated into external modules that
get loaded into the JSP at runtime. Which reminds me, we meant to let
you in on a little secret: JSPs actually get dynamically compiled
into servlets the first time a browser invokes them, so they can do
anything a servlet can do.
This appendix takes you through the process of writing a simple JSP.
We then elaborate on it in several ways, including use of prewritten
JavaBean components and JSP custom tags (also called custom
actions). But we won't discuss
writing any Java components such as servlets,
JavaBeans, or custom tags; that is well beyond the scope of this
book. O'Reilly has a number of good books on these
topics; see http://java.oreilly.com for the latest
catalog. See in particular these O'Reilly books:
Java Servlet Programming by Jason Hunter,
JavaServer Pages by Hans Bergsten, and
Developing Java Beans by Robert Englander.
|
No comments:
Post a Comment