Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Servlet » Servlet API | Packages and Examples of the Servlet API

  • World Environment Day in Hindi : Objective, Importance, Theme
    World Environment Day in Hindi : Objective, Importance, Theme General Knowledge
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
    Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth Biography
  • Newton's laws of Motion, State and Explained, Formula
    Newton’s laws of Motion, State and Explained, Formula Science
  • Pythagorean Theorem in Hindi, Definition
    Pythagorean Theorem in Hindi, Definition, Formula, Proof पाइथागोरस थ्योरम क्या है जानिए हिंदी में? Science
  • Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा
    Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा Festival
  • Gautama Buddha life story in Hindi, Buddha's history
    Gautama Buddha life story in Hindi, Buddha’s history Biography
  • Ramayana Uttar Kand Luv Kush| रामायण उत्तर कांड इन हिंदी
    Ramayana Uttar Kand Luv Kush | रामायण उत्तर कांड इन हिंदी Spiritual
  • Aranya Kand with Hindi Meaning | अरण्यकाण्ड | सीता हरण
    Aranya Kand with Hindi Meaning | अरण्यकाण्ड का अर्थ | सीता हरण Spiritual

Servlet API | Packages and Examples of the Servlet API

Posted on September 1, 2021June 17, 2022 By GeekCer Education
Servlet API

Servlet API consists of two packages which contain all the important classes and interfaces that we use to create servlets.

Java Servlet is just a java class which runs on the web server that contains an interpreter which supports the Java servlet specification.

In this article, we will discuss about servlet API, the packages of servlet API, Servlet Interface, GenericServlet class and HttpServlet class in details. You will have an example of servlet by using those main classes.

First look at the packages of the servlet API, it consists of two packages which are as follows:

  • javax.servlet
  • javax.servlet.http

Table of Contents

  • javax.servlet package of Servlet API
    • Classes of javax.servlet package
    • Interfaces of javax.servlet package
  • javax.servlet.http package of Servlet API
    • Classes of javax.servlet.http package
    • Interfaces of javax.servlet.http package
  • Servlet Interface
    • Methods of Servlet interface
    • Example of servlet by implementing the Servlet interface.
  • GenericServlet Class
    • Example of servlet by extending the GenericServlet class.
  • HttpServlet Class
    • Example of servlet by extending the HttpServlet class.

javax.servlet package of Servlet API

The javax.servlet package contains a list of interfaces and classes that the servlet or web container uses.

Classes of javax.servlet package

There are several classes in the javax.servlet package which are as follows:

  1. GenericServlet
  2. ServletContextAttributeEvent
  3. ServletContextEvent
  4. ServletException
  5. ServletInputStream
  6. ServletOutputStream
  7. ServletRequestAttributeEvent
  8. ServletRequestEvent
  9. ServletRequestWrapper
  10. ServletResponseWrapper
  11. UnavailableException

Interfaces of javax.servlet package

There are several interfaces in the javax.servlet package which are as follows:

  1. Filter
  2. FilterChain
  3. FilterConfig
  4. RequestDispatcher
  5. Servlet
  6. ServletConfig
  7. ServletContext
  8. ServletContextAttributeListener
  9. ServletContextListener
  10. ServletRequest
  11. ServletRequestAttributeListener
  12. ServletRequestListener
  13. ServletResponse
  14. SingleThreadModel

javax.servlet.http package of Servlet API

We use the classes and interfaces of the javax.servlet.http package to interact with the browser using the HTTP protocol. It is only for HTTP requests.

Classes of javax.servlet.http package

There are several classes in the javax.servlet.http package which are as follows:

  1. Filter
  2. Cookie
  3. HttpServlet
  4. HttpServletRequestWrapper
  5. HttpServletResponseWrapper
  6. HttpSessionBindingEvent
  7. HttpSessionEvent
  8. HttpUtils (deprecated now)

Interfaces of javax.servlet.http package

There are several interfaces in the javax.servlet.http package which are as follows:

  1. HttpServletRequest
  2. HttpServletResponse
  3. HttpSession
  4. HttpSessionActivationListener
  5. HttpSessionAttributeListener
  6. HttpSessionBindingListener
  7. HttpSessionContext (deprecated now)
  8. HttpSessionListener

Servlet Interface

The Servlet interface defines some methods that all servlets implement if they use Servlet interface.

Methods of Servlet interface

There are mainly 5 methods in Servlet interface. The init(), service() and destroy() are the methods of servlet life cycle.

#MethodsDescription
1public void init(ServletConfig config) Servlet container calls this method to indicate that the servlet is being placed into service.
2public void service(ServletRequest req, ServletResponse res)This method is called by the servlet container to allow the servlet to respond to a request.
3public void destroy()This method is called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
4public ServletConfig getServletConfig()This method returns a ServletConfig object, which contains initialization and startup parameters for this servlet.
5public String getServletInfo()This method returns information about the servlet, such as author, version, and copyright.

Example of servlet by implementing the Servlet interface.

This program shows you how you can create a servlet by implementing the Servlet interface.

ServletProgram.java

                  import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class ServletProgram implements Servlet{
  ServletConfig config = null;

  public void init(ServletConfig config) {
    this.config = config;
    System.out.println("Initialization Complete");
  }

  public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException {
    res.setContentType("text/html");
    PrintWriter pwriter = res.getWriter();
    pwriter.print("<html>");
    pwriter.print("<body>");
    pwriter.print("<h1>Welcome To GeekCer!</h1>");
    pwriter.print("</body>");
    pwriter.print("</html>");
  }

  public void destroy() {
    System.out.println("Servlet life cycle finished");
  }

  public ServletConfig getServletConfig() {
    return config;
  }

  public String getServletInfo() {
    return "Copyright ©geekcer.com 2021 All Rights reserved";
  }
}

web.xml

                  <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>ServletTutorials</display-name>
	<servlet>
		<servlet-name>ServletProgram</servlet-name>
		<servlet-class>com.geekcer.ServletProgram</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>ServletProgram</servlet-name>
		<url-pattern>/welcome</url-pattern>
	</servlet-mapping>
</web-app>

GenericServlet Class

GenericServlet defines a generic, protocol-independent servlet. It is an abstract class which implements the Servlet and ServletConfig interfaces.

By using GenericServlet class it is easy to write a servlet. It also provides lifecycle method init() and destroy() which is available in ServletConfig interface.

Example of servlet by extending the GenericServlet class.

ServletProgram.java

                  import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class ServletProgram extends GenericServlet{
  public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("Welcome to GeekCer!");
  }
}

web.xml

                  <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>ServletTutorials</display-name>
	<servlet>
		<servlet-name>ServletProgram</servlet-name>
		<servlet-class>com.geekcer.ServletProgram</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>ServletProgram</servlet-name>
		<url-pattern>/welcome</url-pattern>
	</servlet-mapping>
</web-app>

HttpServlet Class

HttpServlet class does not override the service() method, it is more advance class than the GenericServlet class.

We use HttpServlet class to read the HTTP request and then it determines whether the request is HTTP GET, POST, PUT, DELETE, HEAD and calls one of the related method.

Example of servlet by extending the HttpServlet class.

ServletProgram.java

                  import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletProgram extends HttpServlet{
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("Welcome to GeekCer!");
  }
}

web.xml

                  <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>ServletTutorials</display-name>
	<servlet>
		<servlet-name>ServletProgram</servlet-name>
		<servlet-class>com.geekcer.ServletProgram</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>ServletProgram</servlet-name>
		<url-pattern>/welcome</url-pattern>
	</servlet-mapping>
</web-app>

To compare the HttpServlet class and GenericServlet class you can visit the following link:

What is the difference between HttpServlet and GenericServlet?

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Servlet

Post navigation

Previous Post: Steps to Create a Servlet
Next Post: What is WAR file in Java?

More Related Articles

Servlet Tutorial Servlet Tutorial Servlet
What is WAR file in Java What is WAR file in Java? Servlet
Steps to Create a Servlet Steps to Create a Servlet Servlet
HTTP status codes List | Response Status Code Glossary HTTP status codes List | Response Status Code Glossary Important
  • Servlet Home
  • Steps to Create a Servlet
  • Servlet API
  • What is WAR File?
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved