Generic Servlet 

Generic Servlet in Java

In this article, I am going to discuss Generic Servlet in Java with an example. Please read our previous article where we discussed Java Servlet InterfaceAt the end of this article, you will understand the following pointers.

  1. Java Generic Servlet
  2. What are the limitations of the GenericServlet?
  3. What is the general structure of a real servlet (used in real web-based java projects in the industry)?
  4. Methods of Generic Servlet class
  5. Example to demonstrate Generic Servlet
Java Generic Servlet

The GenericServlet implements the Servlet and ServletConfig interfaces. GenericServlet could also be directly extended by a servlet, although it’s more common to increase a protocol-specific subclass like HttpServlet. GenericServlet makes writing servlets easier. It provides simple versions of the lifecycle methods init and destroy and of the methods within the ServletConfig interface. GenericServlet also implements the log method, declared within the ServletContext interface. To write a generic servlet, you need to override the abstract service method.

What are the limitations of the GenericServlet?
  1. It cannot provide HTTP features to user-defined servlets. For example Session tracking, URL rewriting, cookies, etc.
  2. A real-time website should have HTTP features.
  3. Therefore, user defined servlet should not inherit directly from GenericServlet.

Note: javax.servlet.http.HttpServlet which is a sub-class of GenericServlet addresses the limitations of GenericServlet.

What are the limitations of the GenericServlet?

What is the general structure of a real servlet (used in real web-based java projects in the industry)?
public class MyServlet extends HttpServlet
{
    public void init (ServletConfig config) throws ServletException
    {
        //resource allocation code
    }
    
    public void doGet / doPost (HttpServletRequest request, HttpServletResponse) throws IOException,
    ServletException
    {
        // client request handling code
    }	//service method
    
    public void destroy ()
    {
        //resource releasing code
    }
}

Note: When the client request is GET implement doGet() method as a service method in a user-defined servlet. If post then implement doPost() method.

Methods of Generic Servlet class
  1. destroy(): It is called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
  2. getInitParameter(String name): It returns a string containing the value of the named initialization parameter, or null if the parameter does not exist.
  3. getInitParameterNames(): If the servlet has no initialization parameters it returns the names of the servlet’s initialization parameters as an Enumeration of String objects, or an empty Enumeration.
  4. getServletConfig(): It returns this servlet’s ServletConfig object.
  5. getServletContext(): It returns a reference to the ServletContext in which this servlet is running.
  6. getServletInfo(): It returns information about the servlet, such as author, version, and copyright.
  7. getServletName(): It returns the name of this servlet instance.
  8. init(): It is a convenience method that can be overridden so that there’s no need to call super.init(config).
  9. init(ServletConfig config): It is called by the servlet container to indicate to a servlet that the servlet is being placed into service.
  10. log(String msg): It writes the specified message to a servlet lof file, prepended by the servlet’s name.
  11. log(String message, Throwable t): It writes an explanatory message and a stack trace for a given Throwable exception to the servlet log file, prepended by the servlet’s name.
  12. service(ServletRequest req, ServletResponse res): It is called by the servlet container to allow the servlet to respond to a request.
Example to demonstrate Generic Servlet
index.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Generic Servlet Demo</title>
</head>
<body>
    <a href="welcome">Click to call Servlet</a>
</body>
</html>
ServletInterface.java
import java.io.*;
import javax.servlet.*;

public class ServletInterface extends GenericServlet
{
    public void service (ServletRequest req, ServletResponse res) throws IOException, ServletException
    {
        res.setContentType ("text/html");
        PrintWriter pwriter = res.getWriter ();
        pwriter.print ("<html>");
        pwriter.print ("<body>");
        pwriter.print ("<h2>Generic Servlet Example</h2>");
        pwriter.print ("<p>Hello Readers!</p>");
        pwriter.print ("</body>");
        pwriter.print ("</html>");
    }   
}
web.xml
<web-app>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletInterface</servlet-name>
<servlet-class>ServletInterface</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletInterface</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>

Output

Generic Servlet in Java

Click the link on the above screen

Generic Servlet in Java

In the next article, I am going to discuss the HTTP Servlet in Java. Here, in this article, I try to explain Generic Servlet in Java. I hope you enjoy this Generic Servlet in Java article.

Leave a Reply

Your email address will not be published. Required fields are marked *