BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Servlet API

Last Updated: July 22, 2017 by Chaitanya Singh | Filed Under: Java Servlet tutorial

❮ PreviousNext ❯

You need to use Servlet API to create servlets. There are two packages that you must remember while using API, the javax.servlet package that contains the classes to support generic servlet (protocol-independent servlet) and the javax.servlet.http package that contains classes to support http servlet. You may be wondering what is generic and http servlet, I have explained them later in this post.

Let’s see the hierarchy of packages:

java.lang.Object
	|_extended byjavax.servlet.GenericServlet
        	|_extended byjavax.servlet.http.HttpServlet

Every Servlet must implement the java.servlet.Servlet interface, you can do it by extending one of the following two classes: javax.servlet.GenericServlet or javax.servlet.http.HttpServlet. The first one is for protocol independent Servlet and the second one for http Servlet.

How servlet works?
How Servlet Works

Generic Servlet

As I mentioned above, if you are creating a Generic Servlet then you must extend javax.servlet.GenericServlet class. GenericServlet class has an abstract service() method. Which means the subclass of GenericServlet should always override the service() method.
Signature of service() method:

public abstract void service(ServletRequest request, ServletResponse response)
         throws ServletException, java.io.IOException

The service() method accepts two arguments ServletRequest object and ServletResponse object. The request object tells the servlet about the request made by client while the response object is used to return a response back to the client.

Generic Servlet

HTTP Servlet

If you creating Http Servlet you must extend javax.servlet.http.HttpServlet class, which is an abstract class. Unlike Generic Servlet, the HTTP Servlet doesn’t override the service() method. Instead it overrides one or more of the following methods. It must override at least one method from the list below:

  • doGet() – This method is called by servlet service method to handle the HTTP GET request from client. The Get method is used for getting information from the server
  • doPost() – Used for posting information to the Server
  • doPut() – This method is similar to doPost method but unlike doPost method where we send information to the server, this method sends file to the server, this is similar to the FTP operation from client to server
  • doDelete() – allows a client to delete a document, webpage or information from the server
  • init() and destroy() – Used for managing resources that are held for the life of the servlet
  • getServletInfo() – Returns information about the servlet, such as author, version, and copyright.

In Http Servlet there is no need to override the service() method as this method dispatches the Http Requests to the correct method handler, for example if it receives HTTP GET Request it dispatches the request to the doGet() method.

Http Servlet

Interfaces in javax.servlet package

  • Servlet
  • ServletRequest
  • ServletResponse
  • ServletConfig
  • ServletContext
  • SingleThreadModel
  • RequestDispatcher
  • ServletRequestListener
  • ServletRequestAttributeListener
  • ServletContextListener
  • ServletContextAttributeListener
  • Filter
  • FilterConfig
  • FilterChain

Classes in javax.servlet package

  • GenericServlet
  • ServletInputStream
  • ServletOutputStream
  • ServletException
  • ServletRequestWrapper
  • ServletRequestEvent
  • ServletResponseWrapper
  • ServletContextEvent
  • ServletRequestAttributeEvent
  • ServletContextAttributeEvent
  • UnavailableException

Interfaces in javax.servlet.http package

  • HttpSession
  • HttpServletRequest
  • HttpServletResponse
  • HttpSessionAttributeListener
  • HttpSessionListener
  • HttpSessionBindingListener
  • HttpSessionActivationListener
  • HttpSessionContext

Classes in javax.servlet.http package

  • HttpServlet
  • Cookie
  • HttpSessionEvent
  • HttpSessionBindingEvent
  • HttpServletRequestWrapper
  • HttpServletResponseWrapper
  • HttpUtils

References:

  1. javax.servlet Official Servlet API Documentation
  2. java.servlet.http API documentation
❮ PreviousNext ❯

Top Related Articles:

  1. Servlet Interface explained with Example
  2. Servlet Class Hierarchy
  3. Servlet Architecture: Basics of Servlets
  4. Working of Servlet
  5. Servlet Life Cycle

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Servlet Tutorial

  • Servlet Introduction
  • Servlet API
  • Servlet Interface
  • Generic Servlet
  • HttpServlet
  • Servlet in Eclipse
  • Servlet Life cycle
  • Working of Servlet
  • Welcome-file-list
  • load-on-startup tag
  • ServletRequest
  • RequestDispatcher
  • ServletConfig
  • ServletContext
  • ServletResponse
  • HttpSession
  • Cookies
  • Servlet Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap