javax.servlet
Class GenericServlet

java.lang.Object
  |
  +--javax.servlet.GenericServlet
Direct Known Subclasses:
HttpServlet

public abstract class GenericServlet
extends java.lang.Object
implements Servlet, ServletConfig, java.io.Serializable

Abstract base class for all servlets.

Since:
Servlet API 1.0
Version:
Servlet API 2.1
See Also:
Serialized Form

Constructor Summary
GenericServlet()
          Does nothing.
 
Method Summary
 void destroy()
          Called by the server when it no longer needs the servlet.
 java.lang.String getInitParameter(java.lang.String name)
          Gets a servlet's initialization parameter
 java.util.Enumeration getInitParameterNames()
          Gets all the initialization parameters
 ServletConfig getServletConfig()
          Gets the servlet config class
 ServletContext getServletContext()
          Returns the servlets context
 java.lang.String getServletInfo()
          The servlet programmer can put other additional info (version number, etc) here.
 void init()
          Automatically called by init(ServletConfig config).
 void init(ServletConfig config)
          Initializes the servlet.
 void log(java.lang.String message)
          Writes the class name and a message to the log.
 void log(java.lang.String message, java.lang.Throwable th)
          Writes the class name, a message and a stack trace to the log.
abstract  void service(ServletRequest request, ServletResponse response)
          Called by the server every time it wants the servlet to handle a request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericServlet

public GenericServlet()
Does nothing.
Since:
Servlet API 1.0
Method Detail

init

public void init(ServletConfig config)
          throws ServletException
Initializes the servlet. Called by the server exactly once during the lifetime of the servlet. This method can be used to setup resources (connections to a database for example) for this servlet.

This version saves the ServletConfig and calls init(). This means that a servlet can just override init(). Note that if a servlet overrides this method it should call super.init(config) otherwise the other methods in GenericServlet are not garanteed to work.

Specified by:
init in interface Servlet
Parameters:
config - This servlet configuration class
Throws:
ServletException - If an error occurs
Since:
Servlet API 1.0

init

public void init()
          throws ServletException
Automatically called by init(ServletConfig config). This version does nothing.
Throws:
ServletException - If an error occurs
Since:
Servlet API 2.1

getServletContext

public ServletContext getServletContext()
Returns the servlets context
Specified by:
getServletContext in interface ServletConfig
Returns:
The context
Since:
Servlet API 1.0

getInitParameter

public java.lang.String getInitParameter(java.lang.String name)
Gets a servlet's initialization parameter
Specified by:
getInitParameter in interface ServletConfig
Parameters:
name - the name of the wanted parameter
Returns:
the value of the wanted parameter. null if the named parameter is not present.
Since:
Servlet API 1.0

getInitParameterNames

public java.util.Enumeration getInitParameterNames()
Gets all the initialization parameters
Specified by:
getInitParameterNames in interface ServletConfig
Returns:
an Enumeration of all the parameters
Since:
Servlet API 1.0

log

public void log(java.lang.String message)
Writes the class name and a message to the log. Calls getServletContext().log().
Parameters:
message - the message to write
Since:
Servlet API 1.0

log

public void log(java.lang.String message,
                java.lang.Throwable th)
Writes the class name, a message and a stack trace to the log. Calls getServletContext().log().
Parameters:
message - the message to write
th - the object that was thrown to cause this log
Since:
Servlet API 2.1

getServletInfo

public java.lang.String getServletInfo()
The servlet programmer can put other additional info (version number, etc) here.

The Servlet 2.1 Spec says that this should return an empty string. The Servlet 2.1 API says that this should return null unless overridden. This version returns the servlet's class name which seems more usefull.

Specified by:
getServletInfo in interface Servlet
Returns:
The String holding the information
Since:
Servlet API 1.0

getServletConfig

public ServletConfig getServletConfig()
Gets the servlet config class
Specified by:
getServletConfig in interface Servlet
Returns:
The config class
Since:
Servlet API 1.0

service

public abstract void service(ServletRequest request,
                             ServletResponse response)
                      throws ServletException,
                             java.io.IOException
Called by the server every time it wants the servlet to handle a request.
Specified by:
service in interface Servlet
Parameters:
request - all the request information
response - class to write all the response data to
Throws:
ServletException - If an error occurs
java.io.IOException - If an error occurs
Since:
Servlet API 1.0

destroy

public void destroy()
Called by the server when it no longer needs the servlet. The servlet programmer should use this method to free all the resources the servlet is holding.

This version does nothing because it has nothing to clean up.

Note that the the 2.1 Spec says that this should do nothing, but the 2.1 API Doc says that it logs the destroy action.

Specified by:
destroy in interface Servlet
Since:
Servlet API 1.0