GenericServlet class in Servlet with Example

In this tutorial, we will learn about the GenericServlet class and its important methods. We will explain the purpose of all the methods with an example. Before discussing all its methods let's first understand what is GenericServlet class?

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.GenericServlet is protocol independent servlet because it can handle any type of request. GenericServlet class is used to create a generic servlet and providing the implementation of the service() method.
Read: Servlet Interface and its method with Example


Methods of GenericServlet class:

The methods defined by the GenericServlet class are given 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 name param.

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

Example of Servlet by inheriting the GenericServlet class:

In this example, we have created a servlet that inherited the GenericServlet class. If you don't know what is inherited. We will be recommended you to first learn What is Inheritance in Java?

GenericServletExample.java file:

GenericServlet class in Servlet with Example


Web.xml file:
GenericServlet class in Servlet with Example


The output of GenericServlet class Example:
GenericServlet class in Servlet with Example



You may also like:

GenericServlet class in Servlet with Example GenericServlet class in Servlet with Example Reviewed by Prashant Srivastava on October 13, 2018 Rating: 5

No comments:

Powered by Blogger.