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 Class Hierarchy

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: Java Servlet tutorial

The Servlet interface is the root interface of the servlet class hierarchy. All Servlets need to either directly or indirectly implement the Servlet interface. The GenericServlet class of the Servlet API implements the Servlet interface. In addition to the Servlet interface, the GenericServlet class implements the ServletConfig interface of the Servlet API and the Serializable interface of the standard java.io. package. The object of the ServletConfig interface is used by the Web container to pass the configuration information to a servlet when a servlet is initialized.

To develop a servlet that communicates using HTTP, we need to extend the HttpServlet class in our servlet. The HttpServlet class extends the GenericServlet class and provides built-in HTTP functionality.

servlet-architecture

The javax.servlet.Servlet Interface

The servlet interface of the javax.servlet package defines methods that the Web container calls to manage the servlet life cycle. The following table describes various methods of the javax.servlet.Servlet interface:

Method

Description

public void destroy() The Web container calls the destroy() method just before removing the servlet instance from service.
public ServletConfig getServletConfig() Returns a ServletConfig object that contains configuration information, such as initialization parameters, to initialize a servlet.
public String getServletInfo() Returns a string that contains information about the servlet, such as author, version, and copyright.
public void init(ServletConfig config) throws ServletException The Web container calls this method after creating a servlet instance.

The javax.servlet.ServletConfig Interface

The javax.servlet.ServletConfig interface is implemented by a Web container to pass configuration information to a servlet, during initialization of a servlet. A servlet is initialized by passing an object of ServiceConfig to its init() method by the Web container. The ServletConfig object contains initialization information and provides access to the ServletContext object.

Initialization parameters are name value pairs that we can use to pass information to a servlet. The following table lists some of the methods of the javax.servlet.ServletConfig interface:

Method

Description

public String getInitParameter(String param) Returns a String containing the value of the specified initialization parameters or null if the parameter does not exist.
public Enumeration getInitParameterNames() Returns the names of all initialization parameters as an Enumeration of String objects. If no initialization parameters have been defined, an empty Enumeration is returned.
public ServletContext getServletContext() Returns the ServletContext object for the servlet, which allows interaction with the Web container.

Top Related Articles:

  1. Servlet Interface explained with Example
  2. Servlet Life Cycle
  3. HttpServlet class with example
  4. ServletResponse Interface
  5. Servlet Architecture: Basics of Servlets

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