HomeServletsJava Servlet API

Java Servlet API

In this tutorial we discuss about Java Servlet API and its main packages, interfaces and available classes for developing web components.

There are three types of applications available they are: 

1.Stand-alone applications. 

2.Web based applications. 

3.Mobile based applications.

We can develop stand-alone applications by using JSE Edition of java whereas Web applications can be developed using JEE Edition of java (Older) or You can refer to the Jakarta Edition (New).

The servlet API consists of two main packages, we can develop servlet by using these packages. They are as follows.

  1. jakarta.servlet package 
  2. jakarta.servlet.http package 

Servlet API consists of many classes and interfaces which are used to create servlets which are then deployed in webserver that can be accessed from any device.  

Servlet is simply a piece of java code that runs on a web server. How a servlet can be created, accessed and all the operations that can be performed on a servlet is described by the Servlet API. 

1. jakarta.servlet package

Jakarta.servlet package contains several classes and interfaces that describe and define about the link between a servlet class and a run time environment which is provided by the servlet container. We can use this jakarta.servlet package to develop protocol independent servlets (Generic Servlets).

Important Interfaces available in jakarta.servlet package: 


Servlet Consisting of methods that all servlets should implement 
ServletConfig An Object of servlet configuration can be used by the web container send the data to servlet at the time of initialization 
ServletConnection It gives the information about the connection made to the Servlet container 
ServletContext It defines a group of methods that a Servlet is used to communicate with its web container 
ServletRegitration Interface through which every servlet must be configured further 
ServletRequest It provides an object to send the client request information to a servlet 
ServletResponse It provides an object to assist a servlet in sending the response to the client 
RequestDispatcher It is used to dispatch a servlet request to another servlet 
Filter A filter is an object that performs tasks either the request to a resource or on the response from a resource or both 
FilterChain It is an object that is provided by the Servlet container to the developer to give a view of filtered request for a resource 
FilterConfig A filter configuration object is used by the servlet to information of a filter during Initialization 
FilterRegistration It is an interface through which a filter may be further configured  

Important Classes in jakarta.servlet package: 

Class Description 
GenericServlet It implements Servlet and defines a generic, independent Servlet 
ServletInputStream It provides an input stream for receiving binary related data from a client 
ServletOutputStream It provides an output stream for sending the binary data to the client   
ServeltRequestWrapper It implements ServletRequest Interface and provide a convenient implementation to adapt the request of a servlet  
ServletResponseWrapper It implements ServletResponse Interface and provide a convenient implementation to adapt the response of a servlet 
GenericFilter It implements Filter and defines a generic, independent filter  
ServletContextEvent This is the Event class for notifications about changes to servlet context of a web application 
ServletRequestEvent Event of this kind indicate life cycle events for a servlet request 
ServletRequestAttributeEvent This is the event class for notifications about the attributes of servlet request in an application 

Exceptions in jakarta.servlet package: 

Exception Description 
ServletException It gives a servlet exception whenever a servlet find difficulty in processing our request 
UnavailableException It is an Exception that a servlet or a filter throw to indicate that it is temporarily or permanently unavailable  

2. jakarta.servlet.http

jakarta.servlet.http package consists of various classes and interfaces that define and describe the link between a servlet class which is running under HTTP (Hyper Text Transfer Protocol) and a run time environment which is provided by the Servlet container. jakarta.servlet.http package is used to develop Http based servlets.

Important Interfaces in jakarta.servlet.http package: 

Interface Description 
HttpServletRequest It extends the ServletRequest Interface to provide the request information for Http Servlets 
HttpServletResponse It extends the ServletResponse Interface to provide the response information for Http Servlets 
HttpSession It provides a way to identify a user across more than one page request 
WebConnection It is an interface that encapsulates the connection for an upgrade request. 

Important Classes in jakarta.servlet.http package

Class Description 
Cookie This simply creates a cookie, a small amount of information which is sent by a servlet to a web browser. This information is saved by the browser and later sent back to the server 
HttpServlet It is an abstract class which extends GenericServlet to create HTTP servlets for a website 
HttpServletRequestWrapper It implements HttpServletRequest and provide a convenient implementation to adapt the request to a servlet   
HttpServletResponseWrapper It implements HttpServletResponse and provide a convenient implementation to adapt the response from a servlet 
HttpFilter Provides an abstract class to create HTTP based filter for a web site 
HttpSessionEvent This is the class representing event notifications for changes to sessions within a Web application 

3. Conclusion

In this tutorial, we have discussed about the main interfaces and classes in Java Servlet API.