Synopsis
Class Name: javax.servlet.GenericServlet
Superclass: java.lang.Object
Immediate Subclasses: javax.servlet.http.HttpServlet
Interfaces Implemented: javax.servlet.Servlet, javax.servlet.ServletConfig, java.io.Serializable
Availability: Servlet API 1.0 and later
Description
GenericServlet provides a basic implementation of the Servlet interface for protocol-independent servlets. As a convenience, it also implements the ServletConfig interface. Most servlet developers subclass this class or HttpServlet, rather than implement the Servlet interface directly.
GenericServlet includes basic versions of the init( ) and destroy( ) methods, which perform basic setup and cleanup tasks, such as managing the server's ServletConfig object. It's good form for a servlet that overrides one of these methods to call the superclass version of the method. GenericServlet also includes log( ) methods that provide easy access to the logging functions from ServletContext.
The service( ) method is declared as abstract and must be overridden. Well-written servlets also override getServletInfo( ).
Class Summary
public abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable { // Constructors public GenericServlet(); // Instance Methods public void destroy(); public String getInitParameter(String name); public Enumeration getInitParameterNames(); public ServletConfig getServletConfig(); public ServletContext getServletContext(); public String getServletInfo(); public String getServletName(); // New in 2.2 public void init() throws ServletException; // New in 2.1 public void init(ServletConfig config) throws ServletException; public void log(String msg); public void log(String msg, Throwable t); // New in 2.1 public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, IOException; }
Constructors
GenericServlet()
public GenericServlet()
- Description
-
The default GenericServlet constructor does no work. Any servlet initialization tasks should be performed in init( ), rather than in the constructor.
Instance Methods
destroy()
public void destroy()
- Description
-
Called by the servlet container to indicate to a servlet that the servlet has been taken out of service. This method is called only after all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, the container will not call the service method again on the servlet. The default implementation logs the servlet's destruction using the log( ) method. A servlet can override this method to save its state, free its resources (database connections, threads, file handles, etc.), and so forth.
getInitParameter()
public String getInitParameter(String name)
- Description
-
Returns the value of the named servlet initialization parameter or null if no matching parameter is found. From the ServletConfig interface.
getInitParameterNames()
public Enumeration getInitParameterNames()
- Description
-
Returns all the servlet's init parameter names as an Enumeration of String objects or an empty Enumeration if no parameters exist. From the ServletConfig interface.
getServletConfig()
public ServletConfig getServletConfig()
- Description
-
Returns the servlet's ServletConfig object. In practice, this method is rarely called by a GenericServlet because all of the ServletConfig methods are duplicated internally.
getServletContext()
public ServletContext getServletContext()
- Description
-
Returns the servlet's ServletContext object. From the ServletConfig interface.
getServletInfo()
public String getServletInfo()
- Description
-
Returns a programmer-defined String that describes the servlet. A servlet should override this method and provide a customized identity string (e.g., "Al's Message Board Servlet v1.21"), but it is not required.
getServletName()
public String getServletName()
- Description
-
Returns the name of this servlet instance. The name may be provided via server administration, may be assigned in the web application deployment descriptor, or for an unregistered (and thus unnamed) servlet instance will be the servlet's class name. From the ServletConfig interface. This method was introduced in Servlet API 2.2.
init()
public void init() throws ServletException; public void init(ServletConfig config) throws ServletException
- Description
-
Called by the servlet container after the servlet is first loaded and before the servlet's service( ) method is called. A servlet can override this method to perform one-time setup, creation of resources, and so on. Servlets written against Servlet API 2.1 or later can implement the no-argument version. Servlets that must be backward compatible with Servlet API 2.0 should implement the version that takes a ServletConfig parameter. Do not implement both versions. The default implementation of init( ) logs the servlet's initialization and stores the ServletConfig object for use by the methods in the ServletConfig interface. A servlet implementing the version that takes a ServletConfig parameter must call super.init(config) before executing any custom initialization code. Servlets taking advantage of the new no-argument version do not need to worry about this.
log()
public void log(String msg) public void log(String msg, Throwable t)
- Description
-
Writes the given message to a servlet log, prepended by the calling servlet's name. The output location is server-specific, usually an event log file.
service()
public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
- Description
-
Called to handle a single client request. A servlet receives request information via a ServletRequest object and sends data back to the client via a ServletResponse object. This is the only method that must be overridden when extending GenericServlet.