Overview of Servlet API Classes and Interfaces

Servlet API overview

This tutorial provides brief explanation of classes and interfaces of Servlet API.This tutorial is not meant to be a comprehensive reference to Servlet API. But you will learn which classes and interfaces are included in Servlet API and what job they do.

Servlet API is specified in two packages: javax.servlet and javax.servlet.http. The classes and interfaces in javax.servlet are protocol independent, while the classes and interface in javax.servlet.http deals with specialized HTTP Servlets.Some of the classes and interfaces in the javax.servlet.http package extend those specified in javax.servlet package.

The javax.servlet package

The javax.servlet package defines the contract between container and the servlet class. It provides the flexibility to container vendor to implement the API the way they want so long as they follow the specification. To the developers, it provides the library to develop the servlet based applications.

the javax.servlet Interfaces

The javax.servlet package is composed of 14 interfaces.

Servlet interface

The Servlet Interface is the central abstraction of the Java Servlet API. It defines the life cycle methods of a servlet. All the servlet implementations must implement it either directly or indirectly by extending a class which implements the Servlet interface. The two classes in the servlet API that implement the Servlet interface are GenericServlet and HttpServlet. For most purposes, developers will extend HttpServlet to implement their Servlets while implementing web applications employing the HTTP protocol.

ServletRequest interface

ServletRequest interface provides an abstraction of client request.  The object of ServletRequest is used to obtain the client request information such as request parameters, content type and length, locale of the client, request protocol etc. Developers don’t need to implement this interface. The web container provides the implementation this interface.  The web container creates an instance of the implementation class and passes it as an argument when calling the service() method.

ServletResponse interface

The ServletResponse interface assists a servlet in sending a response to the client. Developers don’t need to implement this interface. Servlet container creates the ServletResponse object and passes it as an argument to the servlet’s service() method.

ServletConfig interface

The servlet container uses a ServletConfig object to pass initialization information to the servlet. ServletConfig object is most commonly used to read the initialization parameters. Servlet intialialization parameters are specified in deployment descriptor (web.xml) file. Servlet container passes the ServletConfig object as an argument when calling servlet’s init() method.

ServletContext interface

ServletContext object is used to communicate with the servlet container. There is only one ServletContext object per web application (per JVM). This is initialized when the web application is started and destroyed only when the web application is being shutdown.
ServletContext object is most commonly used to obtain the MIME type of a file, dispatching a request, writing to server’s log file, share the data across application, obtain URL references to resources etc.

SingleThreadModel Interface

SingleThreadModel is a marker interface. It is used to ensure that servlet handle only one request at a time.
Servlets can implement SingleThreadModel interface to inform the container that it should make sure that only one thread is executing the servlet’s service() method at any given moment.

RequestDispatcher Interface

The RequestDispatcher interface is used to dispatch requests to other resources such a servlet, HTML file or a JSP file.

Filter Interface

Filter interface declares life cycle methods of a filter. The life cycle methods include, init() doFilter() and destroy().

FilterChain Interface

The FilterChain object provides the filter with a handle to invoke the rest of the filter chain. Each filter gets access to a FilterChain object when its doFilter() method is invoked. Using this object, the filter can invoke the next filter in the chain.

FilterConfig interface

The FilterConfig interface is similar to ServletConfig interface. It is used to get the initialization parameters.

ServletContextAttributeListner

Implementation of this interface receives notification whenever an attribute is added, removed or replaced in ServletContext. To receive the notifications, the implementation class must be configured in the deployment descriptor (web.xml file).

ServletContextListner Interface

Implementation of this interface receives notification when the context is created and destroyed.

ServletRequestAttributeListner Interface

Implementation of this interface receives notification whenever an attribute is added, removed or replaced in ServletRequest.

ServletRequestListner Interface

Implementation of this interface receives notification whenever the request is coming in and out of scope. A request is defined as coming into scope when it is about to enter the first servlet or filter in each web application, as going out of scope when it exits the last servlet or the first filter in the chain.

The javax.servlet Classes

The javax.servlet package contains 9 classes.

GenericServlet class

The GenericServlet abstract class defines the generic protocol independent servlet. It can be extended to develop our own protocol-based servlet.

ServletContextEvent class

This is the event class for notifications about changes to the servlet context of a web application.

ServletInputStream class

Provides an input stream for reading binary data from a client request.

ServletOutputStream class

Provides an output stream for sending binary data to the client.

ServletRequestAttributeEvent class

This is the event class for notifications of changes to the attributes of ServletRequest in an application.

ServletRequestEvent class

This event class indicates life cycle events of a ServletRequest.

ServletRequestWrapper class

ServletRequestWrapper class provides a convenient implementation of ServletRequest interface.

ServletResponseWrapper class

ServletResponseWrapper class provides a convenient implementation of ServletResponse interface.

javax.servlet Exception classes

The javax.servlet package defines 2 exception classes.

ServletException class

Defines a general exception a servlet can throw when it encounters difficulty.

UnavailableException class

Defines an exception that a servlet or filter throws to indicate that it is permanently or temporarily unavailable.