Servlet technology was the original Java based solution for web development however due to the problems of maintaining the HTML within the Java code they were never a great success. JSP was the solution to this. However as we will see later there is still a place for Servlets in Java Server development.
This topic looks at the architecture of a Servlet and how to go about writing one.
A Servlet is quite simply a java class that adheres to the general model of a Servlet as defined by the Servlet API. A Servlet Container, also known as a Servlet Engine translates requests from whatever protocol is being used into objects that the Servlet understands, and also gives the Servlet an object which it can use to send a response. This container is also responsible for managing the lifecycle of a Servlet.
Now we have already met the idea of a JSP Container which manages JSP execution. In fact the JSP Container is only responsible for the rewriting of a JSP page to a Servlet and then allows the Servlet Container handle the actual execution.
Servlet containers are responsible for handling requests, passing that request to the Servlet and then returning the response to the client. The actual impementation of the container will vary from program to program but the interface between Containers and Servlets is defined by the Servlet API much in the same way as there are many JSP Containers all adhering the to JSP Specification.
The basic lifecycle of a Servlet is,
init() method.service() method.destroy() method.Typically the init() method is only called once and then the service() method
is called repeatedly for each request. This is much more efficient than executing inti(),
service(), destroy() for each request. What happens, you may ask yourself,
when a service() method is still executing when the Container receives another request ?
Typically this will involve the creation of another program execution thread. In practice, Servlet
Containers create a pool of threads to which incoming requests are generally allocated.
You may want to refer to the Java Servlet API documentation while reading this section.
A Servlet is defined by the javax.servlet.Servlet interface. There is also a
GenericServlet abstract class which provides a basic implementation of the Servlet
interface. However for this discusssion we will only look at the HttpServlet class
which extends the GenericServlet class. When you come to write a Servlet it will be
this class that you are most likely to extend.
The service() method is implemented by the HttpServlet as a dispatcher
of HTTP Requests and therefore should never be overridden. When a request is made the service()
method will determine the type of request (GET, POST, etc) and dispacth it to the appropriate method
(doGet(), doPost(), etc). For the most part you will be overridding
doGet() and the doPost() methods. These have message signatures similar
to,
protected void doXxx(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException
We already know the two objects HttpServletRequest and HttpServletResponse
as these are just the implicit objects request and response and can be
manipulated in exactly the same way within the body of the doGet(), doPost()
methods.
The init() method is executed when the Servlet is first instantiated. The Servlet container
will pass an object of type ServletConfig to the method so that container specific configuration data
can be stored by that Servlet instance for leter use.
Not every Servlet requires that the init() method do something. The type of work carried
out by this method can include such activities as initiating database connections, establishing default
values or instantiating JavaBeans. If your Servlet doesn't require any kind of initialiation activity
then don't include an init() method in your Servlet
You can assume that at any given time the Servlet container will decide to remove the Servlet. This might
occur if the Container needs to free some memeory or the Servlet hasn't been requested for some time. The
destroy() method is called prior to removing of the Servlet so you can use this method for
any clean up activity that may be required; releasing database connections etc.
Okay now let's put all this into practice. In the following workshop we want to write a basic Servlet to respond to client requests and generate some output.