|
CSCI A348/A548Lecture Notes 20 Fall 1999 |
The Java Servlet API provides a standard way to extend the
functionality of any kind of server that uses a protocol based
on requests and responses.
Servlets are used primarily with web servers, where
they provide Java-based replacement for CGI scripts.
In other words, on a web server that supports servlets (and there
are many), you can use a Java servlet to create dynamic web content
in much the same way you currently use a CGI script.
Servlets have many advantages over CGI scripts, however:
When a client makes a request involving a servlet, the server loads and executes the appropriate Java classes. Those classes generate content, and the server sends the content back to the client. In most cases, the client is a web browser, the server is a web server, and the servlet returns standard HTML. On the server side, however, there is one important difference: persistence.
Note
however that persistence is used here to mean
"enduring between invocations," and not "written
to permanent storage," as it is sometimes used.
Instead
of shutting down at the end of each request,the servlet can
remain loaded, ready to handle subsequent requests.
The request processing time for a servlet can vary, but it is typically quite fast when compared with a similar CGI program. The real advantage of a servlet, however, is that you incur most of the startup overhead only once.
When a servlet loads, its init() method is called.
You can use init() to create I/O intensive resources,
such as database connections, for use across multiple invocations.
If you have a high-traffic site, the performance benefits can be
quite dramatic. Instead of putting up and tearing down a hundred
thousand database connections, the server needs to create a
connection only once. The servlet's destroy()
method can clean up resources when the server shuts down.
Because servlets are persistent, you can actually remove a lot
of filesystem (and/or database) accesses altogether. For example
to implement a page counter, you can simply store a number in a
static variable rather than consulting a file (or
a database) for every request. Using this technique, you need to
read and write to the disk only occasionally to preserve state.
Since a servlet remains active, it can perform other tasks when it is not servicing client requests, such as running a background processing thread (where clients connect to the servlet to view the result) or even acting as an RMI host, enabling a single servlet to handle connections from multiple types of clients. For example, if you write an order processing servlet, it can accept transactions from both an HTML form and an applet using RMI.
Servlet Basics
The Servlet API consists of two packages:
javax.servlet
javax.servlet.http
javax part is there because
servlets are a standard eXtension to Java,
rather than a mandatory part of the API. This means that while servlets are official Java, Java virtual machine developers are not required to include the classes for them in their Java development and execution environments.
The three core elements of the Servlet API are:
javax.servlet.Servlet interface
javax.servlet.GenericServlet class, and
javax.servlet.http.HttpServlet class
Normally, you create a servlet by subclassing one of the two classes, although if you are adding servlet capability to an existing object, you may find it easier to implement the interface.
The GenericServlet class is used
for servlets that do not implement any particular
communication protocol. Here's a basic servlet that
demonstrates servlet structure by printing a short
message.
We create a script to compile servlets:tucotuco.cs.indiana.edu% pwd /nfs/paca/home/user1/dgerman/November/jserv/servlets tucotuco.cs.indiana.edu% cat BasicServlet.java
import javax.servlet.*; import java.io.*; public class BasicServlet extends GenericServlet { public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); PrintWriter out = resp.getWriter(); out.println("Hello."); } }
tucotuco.cs.indiana.edu%
Then use it to compiletucotuco.cs.indiana.edu% cat compileServlet javac -classpath .:/l/jsdk2.0/lib/jsdk.jar:/l/jdk1.1/lib/classes.zip $1 tucotuco.cs.indiana.edu% date Tue Nov 2 13:11:11 EST 1999
BasicServlet:
And we check it fromtucotuco.cs.indiana.edu% ./compileServlet BasicServlet.java tucotuco.cs.indiana.edu% ls -l BasicServlet.* -rw-r--r-- 1 dgerman students 685 Nov 2 13:11 BasicServlet.class -rw-r--r-- 1 dgerman students 393 Nov 2 13:05 BasicServlet.java -rw-r--r-- 1 dgerman students 374 Nov 2 13:05 BasicServlet.java~ tucotuco.cs.indiana.edu%
http://tucotuco.cs.indiana.edu:59904/servlets/BasicServlet
BasicServlet extends the GenericServlet class
and implements one method: service().
Whenever a server wants
to use the servlet,
service() method,
ServletRequest and ServletResponse objects to it. (We'll look at these types in more detail next week).
The servlet tells the server what type of response to expect, gets a
printWriter from the response object, and transmits its
output.
| ...To be continued in lecture notes 21 (for November 9, 1999). |