< Prev
Next >


GenericServlet is an abstract class which implements Servlet and ServletConfig interface. GenericServlet class can also be used to create a Servlet. GenericServlet class is part of the Servlet API and the full path to import this class is javax.servlet.GenericServlet.

Some ways to create a Servlet

We could create a Servlet in various ways -

You've already seen how to create a Servlet by implementing the Servlet interace. Now let's see how to create a Servlet by extending GenericServlet abstact class.

When to use GenericServlet to create a Servlet?

We could create a Servlet by implementing Servlet interface, even though there is a slight bit of more work with it i.e.. when creating a Servlet by implementing a Servlet class, we must implement all the methods of Servlet interface, even when these methods might not be of any use to our particular Servlet program, these methods are -

We know, GenericServlet is an abstract class which implements Servlet and ServletConfig interface. Hence, GenericServlet class has implemented all the methods of Servlet interface except service(ServletRequest, ServletResponse) method of Servlet interface.

This is why, when creating a Servlet by extending GenericServlet, we only have to implement the service(ServletRequest, ServletResponse) method of Servlet interface and makes the servlet programming easier. This is why GenericServlet class is sometimes preferred over Servlet class when creating a Servlet.

GenericServlet methods

GenericServlet abstract class implements Servlet and ServletConfig interface, hence it has implemented all the methods available in both Servlet and ServletConfig interface except the service(ServletRequest, ServletResponse) of Servlet interface. Hence, this method is left abstract and we must implement it when creating a Servlet by extending the GenericServlet class.

Let's take a look at the methods part of javax.servlet.GenericServlet class.

Methods Description
void init(ServletConfig) This method initializes the Servlet object.
abstract void service(ServletRequest, ServletResponse) This method puts the Servlet in the service.
void destoy() This method destroy the Servlet object.
String getServletInfo() This method gets the Servlet associated information.
ServletConfig getServletConfig() This method gets the ServletConfig object.
String getInitParameter(String str) This method returns the value of a parameter named str
Enumeration getInitParameterNames() This method returns all the parameter names associated with the Servlet.
ServletContext getServletContext() This method returns an object of ServletContext.
String getServletName() This method returns the name of this Servlet object.


Creating the Servlet class by extending GenericServlet

We are creating a Servlet extending the GenericServlet abstract class. GenericServlet class implements Servlet and ServletConfig interface. A Servlet class is just a regular Java class which ends with a .java extension, hence we have named this file
import javax.servlet.*;

public class MyServlet1 extends GenericServlet

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
PrintWriter out = response.getWriter();
out.println("Hello there!");
out.println("Hello from the Servlet made by extending GenericServlet abstract class.");
System.out.println("Putting the Servlet in service");


Directory Structure of Servlet files

The diagram above depicts how to arrange the Servlet files in a specific directory structure, as per Java Servlet Specification-

Creating the Deployment Descriptor file

As per the Java Servlet specifications, every web application based on Servlet must have a Deployment Descriptor file(an XML file) named web.xml. So, let's create one -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""

  <display-name>Welcome tomcat</display-name>
     Welcome tomcat




In deployment descriptor file, <servlet> has two child tags <servlet-name> and <servlet-class> :

Setting the classpath

Much of the support for developing the web applications based on the Java Servlet technology does not come with the core Java. Hence, in order to compile the Servlet programs, we have to set the classpath to a jar file named servlet-api.jar.

This jar file provides all the classes that are required for the Servlet programming and it comes within the lib Folder of Tomcat installation folder.

For example, in our case we have installed Tomcat Web Server within the C: Drive, hence the path to our lib folder containing the servlet-api.jar is - C:\apache-tomcat-9.0.2\lib

There are two ways to set the classpath -

Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition