Open In App

Servlet API

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

Classes available in javax.servlet package: 



Class Name

Description

GenericServlet To define a generic and protocol-independent servlet.
ServletContextAttributeEvent  To generate notifications about changes to the attributes of the servlet context of a web application.
ServletContextEvent To generate notifications about changes to the servlet context of a web application.
ServletInputStream This class provides an input stream to read binary data from a client request.
ServletOutputStream This class provides an output stream for sending binary data to the client.
ServletRequestAttributeEvent To generate notifications about changes to the attributes of the servlet request in an application.
ServletRequestEvent To indicate lifecycle events for a ServletRequest.
ServletRequestWrapper This class provides the implementation of the ServletRequest interface that can be subclassed by developers to adapt the request to a Servlet.
ServletResponseWrapper This 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

Filter To perform filtering tasks on either the request to a resource, or on the response from a resource, or both.
FilterChain To provide a view into the invocation chain of a filtered request for a resource to the developer by the servlet container.
FilterConfig To pass information to a filter during initialization used by a servlet container.
RequestDispatcher It 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.
Servlet This 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.
ServletConfig It defines an object created by a servlet container at the time of servlet instantiation and to pass information to the servlet during initialization.
ServletContext It 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.
ServletContextAttributeListener The classes that implement this interface receive notifications of changes to the attribute list on the servlet context of a web application.
ServletContextListener The classes that implement this interface receive notifications about changes to the servlet context of the web application they are part of.
ServletRequest It defines an object that is created by servlet container to pass client request information to a servlet.
ServletRequestAttributeListener To generate the notifications of request attribute changes while the request is within the scope of the web application in which the listener is registered.
ServletRequestListener To generate the notifications of requests coming in and out of scope in a web component.
ServletResponse It 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

ServletException A general exception thrown by a servlet when it encounters difficulty.
UnavailableException Thrown by a servlet or filter to indicate that it is permanently or temporarily unavailable.

javax.servlet.http

Classes available in javax.servlet.http package: 

Class Name

Description

Cookie Creates 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.
HttpServlet Provides an abstract class that defines methods to create an HTTP suitable servlet for a web application.
HttpServletRequestWrapper This class provides implementation of the HttpServletRequest interface that can be subclassed to adapt the request to a Servlet.
HttpServletResponseWrapper This class provides implementation of the HttpServletResponse interface that can be subclassed to adapt the response from a Servlet.
HttpSessionBindingEvent This 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.
HttpSessionEvent To represent event notifications for changes to sessions within a web application.

Interfaces available in javax.servlet.http package: 

Interface Name

Description

HttpServletRequest To provide client HTTP request information for servlets. It extends the ServletRequest interface.
HttpServletResponse To provide HTTP-specific functionality in sending a response to client. It extends the ServletResponse interface.
HttpSession It provides a way to identify a user across web application/web site pages and to store information about that user.
HttpSessionActivationListener Container to notify all the objects that are bound to a session that sessions will be passivated and that session will be activated.
HttpSessionAttributeListener To get notifications of changes to the attribute lists of sessions within this web application, this listener interface can be implemented.
HttpSessionBindingListener It causes an object to be notified by an HttpSessionBindingEvent object, when it is bound to or unbound from a session.
HttpSessionListener To 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.




public abstract class GenericServlet
extends java.lang.Object
implements Servlet, ServletConfig, java.io.Serializable

HttpServlet Class

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




public abstract class HttpServlet
extends GenericServlet
implements java.io.Serializable

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

Servlet API Package Hierarchy

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

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.


Article Tags :