GenericServlet Class


GenericServlet Tutorial class implements Servlet, ServletConfig and Serializable interfaces. It offers the implementation of all of the strategies of those interfaces besides the carrier approach.

GenericServlet tutorial class can take care of any form of request so it's far protocol-independent.

Also, You can also additionally create a typical servlet with the aid of inheriting the GenericServlet class and presenting the implementation of the carrier approach.

Methods of GenericServlet Class

There are many strategies in GenericServlet magnificence. And all are given below:

  1. public void init(ServletConfig config) is used to start the servlet.
  2. public summary void carrier(ServletRequest request, ServletResponse response) offers carrier for the incoming request. It is invoked on every occasion while a person requests for a servlet.
  3. public void destroy() is invoked handiest as soon as in the course of the existence cycle and shows that servlet is being destroyed.
  4. public ServletConfig getServletConfig() returns the item of ServletConfig.
  5. public String getServletInfo() returns statistics approximately servlet together with writer, copyright, model etc.
  6. public void init() it's far a handy approach for the servlet programmers, now there may be no want to name super.init(config)
  7. public ServletContext getServletContext() returns the item of ServletContext.
  8. public String getInitParameter(String call) returns the parameter fee for the given parameter call.
  9. public Enumeration getInitParameterNames() returns all of the parameters described withinside the web.xml report.
  10. public String getServletName() returns the call of the servlet item.
  11. public void log(String msg) writes the given message withinside the servlet log report.
  12. public void log(String msg,Throwable t) writes the explanatory message withinside the servlet log report and a stack trace.

Inheriting the GenericServlet Class with an Example:

Here's the common and easy example of servlet by assuming the GenericServlet Class:

First: First.java

import java.io.*;  

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>");