Servlets - SPLessons

Generic Servlet Class

Chapter 27

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Generic Servlet Class

Generic Servlet Class

shape Description

Generic Servlet Class, GenericServlet class implements Servlet, ServletConfig and Serializable interfaces. GenericServlet implements Servlet Interface. It implements all the methods of Servlet Interface except service method. GenericServlet can be defined as a generics, it can handle any type of request so it is a protocol-independent servlet. GenericServlet implements log method from ServletContext Interface. When generic is written user can override the abstract service method. Following are the methods of GenericServlet.

shape Methods

Methods Description
init(ServletConfig config) Initializes the servlet and the return type is void (not return any data type).
Service(ServletRequest request, ServletResponse response) Provides a server to the incoming request. Calls server every time to producresponse by using servlet and the return type is void.
destroy() Cleans up the servlets service and the return type is void.
getServletConfig() Gets the  servlet config object and the return type is ServletConfig.
getServletInfo() Returns information about servlet such as writer, copyright, version.
init() It is a helpful technique for the servlet software engineers, now there is no compelling reason to call super.init(config).
getServletContext() Returns the ServletContext object.
getInitParameter(String name) Gives back the parameter value for the given parameter name.
getInitParameterNames() It returns  parameter values which are declared in web.xml file.
public Enumeration getInitParameterNames() Returns all the parameters which are declared in web.xml file.
getServletName() Returns back the name of the servlet object and the return type is string .
log(String msg) Writes the class name and a message to the log and the return type is log.

shape Example

 welcome.html

<html>
	<body>
		<form action="./go">
			<input type="submit" value="submit here"/>
		</form>
	</body>
</html>

Here the developer just used submit button that is submit here.
web.xml

<web-app>

  <servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>ServletDemo</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/go</url-pattern>
  </servlet-mapping>
  
</web-app>

Make sure that URL name should be match with HTML form and should be also same, if the developer use any package name then package name also should be mentioned.
ServletDemo.java

package servletgenericservlet;

import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
 
 
public class ServletDemo extends GenericServlet {
     
    public void init() throws ServletException {
        // Put your code here
    }
 
    @Override
    public void service(ServletRequest request, ServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
 
        response.setContentType("text/html");  
           
        PrintWriter out=response.getWriter();
 
        out.print("<html><body>");  
        out.print("<h1><b>welcome to Splessons</b></h1>"); 
        out.print("<h2><b>example on GenericsServlets</b></h2>"); 
        out.print("</body></html>");  
           
        }  
 
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }
     
}

The init() method is intended to be called just once. It is called when the servlet is initially made, and not called again for every client demand. Along these lines, it is utilized for one-time initializations, pretty much as with the init strategy for applets. The servlet is typically made when a client first conjures a URL relating to the servlet, however you can likewise indicate that the servlet be stacked when the server is initially begun.

The destroy() method is called just once toward the end of the life cycle of a servlet. This technique allows servlet to close database connections and it makes the object garbage collected.

Output
After compiling the above code following page will be displayed where click on the button.

When click on button following message will be displayed.


GenericServlet vs HttpServlet

shape Description

The following are the some important differences between GenericServlet vs HttpServlet.

  • GenericServlet gives abstract service(ServletRequest, ServletResponse) technique to actualize which gets called by container at whatever point it gets ask for handling, On the other hand HttpServlet supersedes servicestrategy and gives callback on doGet and doPost at whatever point it gets HTTP request for GET or POST strategy. It additionally gives a few other technique in light of different HTTP methods for sending request.
  • Generic servlet gives a easy approach to expand Servlet, its enough to supersede benefit technique to actualize GenericServlet. Aside from expanding Servlet interface, it additionally executes ServletConfig interface and gives approach to acknowledge introduction parameter go to Servlet from web.xml e.g. by utilizing getInitParamter().
  • Summary

    shape Key Points

    • Generic Servlet Class – GeneriServlet is a class utilized to implement ServletConfig.
    • GeneriServlet is a capable of accept any request.
    • GeneriServlet class is protocol independent.