Servlet class Hierarchy and API
A Servlet, in its most general form, is an instance of a class which implements the javax.servlet.Servlet interface. Most Servlets, however, extend one of the standard implementations of that interface, namely the classes javax.servlet.GenericServlet and javax.servlet.http.HttpServlet.
Servlet class Hierarchy
Servlet interface is at the top of the Servlet Hierarchy. It contains the life cycle methods namely, init (), service () and destroy (). It also contains getServletConfig() and getServletInfo() methods. GenericServlet is an abstract class that implements most of the basic Servlet methods we will need, including those from the Servlet interface. HttpServlet is also an abstract class that extends the GenericServlet class. This class implements the service() method in a HTTP-specific request and response based manner. It also implements the doGet() and doPost() methods. The figure below shows the Servlet hierarchy with important methods for each of the interface and classes using UML class diagram:
Figure: The Servlet class Hierarchy
In order to initialize a Servlet, a server application loads the Servlet class (and probably other classes which are referenced by the Servlet) and creates an instance by calling the no-args constructor. Then it calls the Servlet’s init (ServletConfig config) method. The Servlet should perform one-time setup procedures in this method and store the ServletConfig object so that it can be retrieved later by calling the Servlet’s getServletConfig() method. This is handled by GenericServlet. Servlets which extend GenericServlet (or its subclass HttpServlet) should call super.init (config) at the beginning of the init method to make use of this feature.
The ServletConfig object contains Servlet parameters and a reference to the Servlet’s ServletContext. The init method is guaranteed to be called only once during the Servlet’s life cycle. It does not need to be thread-safe because the service method will not be called until the call to init returns.
When the Servlet is initialized, its service (ServletRequest request, ServletResponse response) method is called for every request to the Servlet. The method is called concurrently (i.e. multiple threads may call this method at the same time) so it should be implemented in a thread-safe manner.
When the Servlet needs to be unloaded (e.g. because a new version should be loaded or the server is shutting down) the destroy() method is called. There may still be threads that execute the service method when destroy is called, so destroy has to be thread-safe. All resources which were allocated in init should be released in destroy. This method is guaranteed to be called only once during the Servlet’s life cycle.
The Java Servlet API is a class library for Servlets. Servlets are Java programs that run on the server and send response to the client. Two packages are available for Servlet programmers: 1) javax.servlet and 2) javax.servlet.http.
The first one contains basic classes and interfaces that we can use to write Servlets from the scratch. The second package, javax.servlet.http, offers more advanced classes and interfaces that extend classes and interfaces from the first package. It is much more convenient to program using the second package.
The javax.servlet package contains a number of classes and interfaces that describe and define the contracts between a servlet class and the runtime environment provided for an instance of such a class by a conforming servlet container.
The javax.servlet.http package contains a number of classes and interfaces that describe and define the contracts between a servlet class running under the HTTP protocol and the runtime environment provided for an instance of such a class by a conforming servlet container.
1) Package javax.servlet
Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server.
Defines methods that all servlets must implement.
A servlet configuration object used by a servlet container used to pass information to a servlet during initialization.
Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.
Defines an object to provide client request information to a servlet.
Defines an object to assist a servlet in sending a response to the client.
Ensures that Servlets handle only one request at a time.
Defines a generic, protocol-independent servlet.
Provides an input stream for reading binary data from a client request, including an efficient readLine method for reading data one line at a time.
Provides an output stream for sending binary data to the client.
Defines a general exception a servlet can throw when it encounters difficulty.
Defines an exception that a servlet throws to indicate that it is permanently or temporarily unavailable.
2) Package javax.servlet.http
Extends the ServletRequest interface to provide request information for HTTP servlets.
Extends the ServletResponse interface to provide HTTP-specific functionality in sending a response.
Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user.
Causes an object to be notified when it is bound to or unbound from a session.
Deprecated. As of Java(tm) Servlet API 2.1 for security reasons, with no replacement.
Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. Used to store small amount of information of client side.
Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site.
Sent to an object that implements the interface HttpSessionBindingListener when the object is bound to or unbound from the session.
Provides a collection of methods that are useful in writing HTTP servlets.
Important Methods of the Servlet Interface:
Called by the Servlet container to indicate to a Servlet that the servlet is being taken out of service.
Returns a ServletConfig object, which contains initialization and startup parameters for this servlet.
Returns information about the Servlet, such as author, version, and copyright.
void init(ServletConfig config)
Called by the servlet container to indicate to a Servlet that the servlet is being placed into service.
void service (ServletRequest req, ServletResponse res)
Called by the Servlet container to allow the servlet to respond to a request.