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

  • Most Popular Hibernate Interview Questions and Answers
    Most Popular Hibernate Interview Questions and Answers Hibernate
  • JSP Directives
    JSP Directives JSP
  • Function in C Language | Recursive function with example
    Function in C Language | Recursive function with example C Language
  • Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा
    Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा Festival
  • Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti
    Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti Spiritual
  • Kumkum Bhagya Hindi TV Serial, Cast Name, Actress Name
    Kumkum Bhagya Hindi TV Serial, Cast Name, Actress Name News
  • Object Oriented Programming
    Object Oriented Programming Java
  • Apj Abdul Kalam biography in Hindi, Life, Missile Man of India
    Apj Abdul Kalam biography in Hindi, Life, Missile Man of India Biography

Servlet API | Packages and Examples of the Servlet API

Posted on September 1, 2021June 17, 2022 By GeekCer Education
Servlet API | Packages and Examples of the 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

HTTP status codes List | Response Status Code Glossary HTTP status codes List | Response Status Code Glossary Important
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

Related Posts

  • HTTP status codes List | Response Status Code Glossary
    HTTP status codes List | Response Status Code Glossary Important
  • 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
  • Servlet Home
  • Steps to Create a Servlet
  • Servlet API
  • What is WAR File?

Recent Posts

  • National Doctors Day in India and Other Countries, July 1, 2022
  • America Independence Day : 4th July USA | USA Birthday
  • Ganesh Chaturthi Puja in Hindi | गणेश चतुर्थी का व्रत, महत्व, कथा
  • जन्माष्टमी व्रत पूजा विस्तार से | दही हांडी | Krishna Janmashtami Puja
  • Jagannath Rath Yatra History in Hindi | जगन्नाथ पुरी की कहानी
  • Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी
  • Internal Working of HashMap in Java, Rehashing, Collision
  • MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi
  • Most Popular Hibernate Interview Questions and Answers
    Most Popular Hibernate Interview Questions and Answers Hibernate
  • JSP Directives
    JSP Directives JSP
  • Function in C Language | Recursive function with example
    Function in C Language | Recursive function with example C Language
  • Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा
    Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा Festival
  • Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti
    Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti Spiritual
  • Kumkum Bhagya Hindi TV Serial, Cast Name, Actress Name
    Kumkum Bhagya Hindi TV Serial, Cast Name, Actress Name News
  • Object Oriented Programming
    Object Oriented Programming Java
  • Apj Abdul Kalam biography in Hindi, Life, Missile Man of India
    Apj Abdul Kalam biography in Hindi, Life, Missile Man of India Biography
  • 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