Understanding the GenericServlet class
The GenericServlet class
GenericServlet class defines a generic protocol independent servlet. It is protocol independent in the sense it can be extended to provide implementation of any protocol like FTP, SMTP etc. Servlet API comes with HttpServlet class which implements HTTP protocol. Generally when developing web application, you will never need to write a servlet that extends GenericServlet. Most of the Servlets in your application will extend HttpServlet class to handle web requests.
GenericServlet class provides implementation of Servlet Interface. This is an abstract class, all the subclasses must implement service () method.
Public abstract class GenericServlet implements Servlet,ServletConfig,Serializable
In addition to those methods defined in Servlet Interface, GenericServlet defines following additional methods.
Public void init()
Public void log(String message)
Public void log(String message, Throwable t)
Initializing the Servlet
Public void init(ServletConfig config)
Public void init()
Public void init(ServletConfig config) method is an implementation of init() method defined in Servlet interface, it stores the ServletConfig object in private transient instance variable. getServletConfig() method can be used to get the reference of this object. If you override this method, you should include a class to super.init(config) method. Alternatively, you can override no-argument init() method.
Handling requests
Public abstract void service (ServletRequest request, ServletResponse response);
This is an abstract method, and you must override this method in your subclass to handle requests. All the code related to request processing and response generation goes here.
Destroying the Servlet
Public void destroy()
This method provides default implementation of destroy () method defined in the Servlet interface. You can override this method in your subclasses to do some cleanup tasks when servlet is taken out of service.
Accessing the environment
GenericServlet class also implements ServletConfig interface so all the methods of ServletConfig interface can be called directly without first obtaining reference to ServletConfig instance.
Public string getInitParameter(String name) : used to obtaing value of servlet initialization parameter.
Public String getInitParameterNames() : returns names of all initialization parameters.
Public ServletContext getServletContext() : returns reference to ServletContext object.
getServletName() : used to obtain name of the servlet.
Writing to server log file
Public void log(String message)
Public void log(String message, Throwable t)
GenericServlet class provides two utility methods for writing to server log file. log(String message) method writes servlet name and message to web containers log file. the other method log(string message, Throwable t), writes servlet name, string message and the exception stack trace of the given Throwable exception to web containers log file.
Generic Servlet Example
package com.jsptube.servlet;
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 GenericServletExample extends GenericServlet {
public void init() {
log("inside init() method");
}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
log("Handling request");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.write("<html><head><title>GenericServle example</title></head>");
out.write("<body><h1>GenericServlet: Hallo world </h1></body></html>");
out.close();
}
public void destroy() {
log("inside destroy() method");
}
}
Web.xml deployment descriptor
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>GenericServlet</servlet-name>
<servlet-class>com.jsptube.servlet.GenericServletExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GenericServlet</servlet-name>
<url-pattern>/exampleservlet</url-pattern>
</servlet-mapping>
</web-app>
