Servlet GenericServlet

In this tutorial you will learn about the Servlet GenericServlet and its application with practical example.

GenericServlet Class

GenericServlet is protocol independent, it means it can handle any type of request. It’s direct known subclass is HttpServlet. GenericServlet implements the Servlet , ServletConfig and Serializable interfaces. It can be directly extended by a servlet.

Methods In GenericServlet Class

There are some important methods are listed below.
public void init(ServletConfig config):- This method called by servlet container which indicates that servlet is being placed into service.
public String getInitParameter(String name):– This method is used to returns a string which contain value of the named initialization parameter or null( if the parameter does not exist.)
public abstract void service(ServletRequest request, ServletResponse response):- This method called by Servlet container to allow the servlet to respond to a request.
public void destroy():- This method called by the servlet container to indicates to a servlet is being taken out of service.
public ServletConfig getServletConfig():- This method returns a ServletConfig object, which contains initialization and startup parameter for this Servlet.
public String getServletInfo():- This method returns information about the Servlet, like author, version, and copyright. The string that this methods returns must be plain text.
public ServletContext getServletContext():- This method returns a reference to the ServletContext in which this servlet running.
public String getServletName(): This method returns the name of the this servlet instance.
public void log(String msg):- This method writes the specified message to servlet log file.
public void log(String msg,Throwable t):- This method is used to writes an explanatory message and stack trace for given Throwable exception to the servlet log file.
public void init():- This is a convenience method which can be overridden so that there is no need to call super.init(config).
public Enumeration getInitParameterNames():- This method returns the names of the parameters which are defined in the web.xml file.

Example:-

Let’s understand this class by an example. In this example we have inherited GenericServlet class.

Output:-

Servlet GenericServlet

 

 

dgdfgd

In this tutorial we have learn about the Servlet GenericServlet and its application with practical example. I hope you will like this tutorial.