Open In App

Servlet API

Last Updated : 11 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Servlets are the Java programs that run on the Java-enabled web server or application server. They are used to handle the request obtained from the webserver, process the request, produce the response, then send a response back to the webserver. In Java, to create web applications we use Servlets. To create Java Servlets, we need to use Servlet API which contains all the necessary interfaces and classes. Servlet API has 2 packages namely,

  • javax.servlet
  • javax.servlet.http

javax.servlet

  • This package provides the number of interfaces and classes to support Generic servlet which is protocol independent.
  • These interfaces and classes describe and define the contracts between a servlet class and the runtime environment provided by a servlet container.

Classes available in javax.servlet package: 

Class Name

Description

GenericServletTo define a generic and protocol-independent servlet.
ServletContextAttributeEvent To generate notifications about changes to the attributes of the servlet context of a web application.
ServletContextEventTo generate notifications about changes to the servlet context of a web application.
ServletInputStreamThis class provides an input stream to read binary data from a client request.
ServletOutputStreamThis class provides an output stream for sending binary data to the client.
ServletRequestAttributeEventTo generate notifications about changes to the attributes of the servlet request in an application.
ServletRequestEventTo indicate lifecycle events for a ServletRequest.
ServletRequestWrapperThis class provides the implementation of the ServletRequest interface that can be subclassed by developers to adapt the request to a Servlet.
ServletResponseWrapperThis class provides the implementation of the ServletResponse interface that can be subclassed by developers to adapt the response from a Servlet.

Interfaces available in javax.servlet package: 

Interface Name

Description

FilterTo perform filtering tasks on either the request to a resource, or on the response from a resource, or both.
FilterChainTo provide a view into the invocation chain of a filtered request for a resource to the developer by the servlet container.
FilterConfigTo pass information to a filter during initialization used by a servlet container.
RequestDispatcherIt defines an object to dispatch the request and response to any other resource, means it receives requests from the client and sends them to a servlet/HTML file/JSP file on the server.
ServletThis is the main interface that defines the methods in which all the servlets must implement. To implement this interface, write a generic servlet that extends javax.servlet.GenericServlet or an HTTP servlet that extends javax.servlet.http.HttpServlet.
ServletConfigIt defines an object created by a servlet container at the time of servlet instantiation and to pass information to the servlet during initialization.
ServletContextIt defines a set of methods that a servlet uses to communicate with its servlet container. The information related to the web application available in web.xml file is stored in ServletContext object created by container.
ServletContextAttributeListenerThe classes that implement this interface receive notifications of changes to the attribute list on the servlet context of a web application.
ServletContextListenerThe classes that implement this interface receive notifications about changes to the servlet context of the web application they are part of.
ServletRequestIt defines an object that is created by servlet container to pass client request information to a servlet.
ServletRequestAttributeListenerTo generate the notifications of request attribute changes while the request is within the scope of the web application in which the listener is registered.
ServletRequestListenerTo generate the notifications of requests coming in and out of scope in a web component.
ServletResponseIt defines an object created by servlet container to assist a servlet in sending a response to the client.

Exceptions in javax.servlet package: 

Exception Name

Description

ServletExceptionA general exception thrown by a servlet when it encounters difficulty.
UnavailableExceptionThrown by a servlet or filter to indicate that it is permanently or temporarily unavailable.

javax.servlet.http

  • This package provides the number of interfaces and classes to support HTTP servlet which is HTTP protocol dependent.
  • These interfaces and classes describe and define the contracts between a servlet class running under HTTP protocol and the runtime environment provided by a servlet container.

Classes available in javax.servlet.http package: 

Class Name

Description

CookieCreates a cookie object. It is 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 for session management.
HttpServletProvides an abstract class that defines methods to create an HTTP suitable servlet for a web application.
HttpServletRequestWrapperThis class provides implementation of the HttpServletRequest interface that can be subclassed to adapt the request to a Servlet.
HttpServletResponseWrapperThis class provides implementation of the HttpServletResponse interface that can be subclassed to adapt the response from a Servlet.
HttpSessionBindingEventThis events are either sent to an object that implements HttpSessionBindingListener when it is bound or unbound from a session, or to a HttpSessionAttributeListener that has been configured in the deployment descriptor when any attribute is bound, unbound or replaced in a session.
HttpSessionEventTo represent event notifications for changes to sessions within a web application.

Interfaces available in javax.servlet.http package: 

Interface Name

Description

HttpServletRequestTo provide client HTTP request information for servlets. It extends the ServletRequest interface.
HttpServletResponseTo provide HTTP-specific functionality in sending a response to client. It extends the ServletResponse interface.
HttpSessionIt provides a way to identify a user across web application/web site pages and to store information about that user.
HttpSessionActivationListenerContainer to notify all the objects that are bound to a session that sessions will be passivated and that session will be activated.
HttpSessionAttributeListenerTo get notifications of changes to the attribute lists of sessions within this web application, this listener interface can be implemented.
HttpSessionBindingListenerIt causes an object to be notified by an HttpSessionBindingEvent object, when it is bound to or unbound from a session.
HttpSessionListenerTo receive notification events related to the changes to the list of active sessions in a web application.

GenericServlet Class

Servlet API provide GenericServlet class in javax.servlet package.

Java
public abstract class GenericServlet
extends java.lang.Object
implements Servlet, ServletConfig, java.io.Serializable
  • An abstract class that implements most of the servlet basic methods.
  • Implements the Servlet, ServletConfig, and Serializable interfaces.
  • Protocol-independent servlet.
Generic Servlet API Execution Flow
  • Makes writing servlets easier by providing simple versions of the lifecycle methods init() and destroy().
  • To write a generic servlet, you need to extend javax.servlet.GenericServlet class and need to override the abstract service() method.

HttpServlet Class

Servlet API provides HttpServlet class in javax.servlet.http package.

Java
public abstract class HttpServlet
extends GenericServlet
implements java.io.Serializable
  • An abstract class to be subclassed to create an HTTP-specific servlet that is suitable for a Web site/Web application.
  • Extends Generic Servlet class and implements Serializable interface.
  • HTTP Protocol-dependent servlet.
HTTP Servlet API Execution Flow

To write a Http servlet, you need to extend javax.servlet.http.HttpServlet class and must override at least one of the below methods,

  • doGet() – to support HTTP GET requests by the servlet.
  • doPost() – to support HTTP POST requests by the servlet.
  • doPut() – to support HTTP PUT requests by the servlet.
  • doDelete() – to support HTTP DELETE requests by the servlet.
  • init() and destroy() – to manage resources held in the life of the servlet.
  • getServletInfo() – To provide information about the servlet itself like the author of servlet or version of it etc.

Servlet API Package Hierarchy

Below is the package hierarchy of Generic Servlet and HTTP Servlet.

Servlet API Package Hierarchy

Servlet API provides all the required interfaces, classes, and methods to develop a web application. we need to add the servlet-api.jar file in our web application to use the servlet functionality. We can download this jar file from Maven Repository.



Next Article
Article Tags :
Practice Tags :

Similar Reads