GenericServlet class in Servlet with Example | Definition, Hierarchy, Methods & Sample Code of Generic Servlet

In this tutorial, we will discover what is GenericServlet class and its important methods? Also, we will explain all GenericServelt class methods’ purposes with an example. Let’s start with the understanding of GenericServlet class definition first and then move forward to know about methods of GenericServlet class in Servlet with Example.

What is the GenericServlet class?

GenericServlet class provides implementations of the basic life cycle method for a servlet and is typically subclassed by the servlet developers. GenericServlet implements servlet and ServletConfig interfaces. It is a protocol-independent servlet as it can handle any type of request. GenericServlet class is used to create a generic servlet and providing the implementation of the service() method.

How Generic Servlet works?

working of genericservlet class

              |_extended byjavax.servlet.GenericServlet

Also Read:

Methods of GenericServlet class

The methods illustrated by the GenericServlet class are provided below:

1. public void init(ServletConfig sc) throws ServletException: It is called when the servlet initialized. Initialization parameter for the servlet can be obtained from sc. An UnavailableException should be thrown if the servlet cannot be initialized.

2. public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException: The Service() method is called to process a request from a client. The request from a client can be read from req. The response to the client can be written to res. An Exception is generated if some IO and Servlet problems occur.

3. public void destroy(): The server calls the destroy() method after the servlet has been taken out of service and all pending requests to the servlet have completed or time out.

4. public String getServletInfo(): This method returns a string describing the servlet.

5. public ServletConfig getServletConfig(): This method returns a ServletConfig object that contains any initialization parameters.

6. public ServletContext getServletContext(): It returns the context for this servlet.

7. public void log(String str): It is used to writes str to the servlet log.

8. public void log(String str, Object obj): It sets the attribute specified by str to the value passed in obj.

9. public String getServletName(): This method is used to returns the name of the invoking servlet.

10. public String getInitParameter(String param): It returns the value of the initialization parameter named param.

11. public Enumeration getInitParameterNames(): It returns an enumeration of all initialization parameter names.

Servlet Example by inheriting the GenericServlet class

In this example, we have built a servlet that inherited the GenericServlet class. If you don’t know what is inherited.

import javax.servlet.*; 

public class First extends GenericServlet{ 
public void service(ServletRequest req,ServletResponse res) 
throws IOException,ServletException{ 


PrintWriter out=res.getWriter(); 
out.print("<b>hello generic servlet</b>");