All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class javax.servlet.GenericServlet

java.lang.Object
   |
   +----javax.servlet.GenericServlet

public abstract class GenericServlet
extends Object
implements Servlet, ServletConfig, Serializable
The GenericServlet class implements the Servlet interface and, for convenience, the ServletConfig interface. Servlet developers typically subclass GenericServlet, or its descendent HttpServlet, unless the servlet needs another class as a parent. (If a servlet does need to subclass another class, the servlet must implement the Servlet interface directly. This would be necessary when, for example, RMI or CORBA objects act as servlets.)

The GenericServlet class was created to make writing servlets easier. It provides simple versions of the lifecycle methods init and destroy, and of the methods in the ServletConfig interface. It also provides a log method, from the ServletContext interface. The servlet writer must override only the service method, which is abstract. Though not required, the servlet implementer should also override the getServletInfo method, and will want to specialize the init and destroy methods if expensive servlet-wide resources are to be managed.


Constructor Index

 o GenericServlet()
The default constructor does no work.

Method Index

 o destroy()
Destroys the servlet, cleaning up whatever resources are being held, and logs the destruction in the servlet log file.
 o getInitParameter(String)
Returns a string containing the value of the named initialization parameter, or null if the requested parameter does not exist.
 o getInitParameterNames()
Returns the names of the servlet's initialization parameters as an enumeration of strings, or an empty enumeration if there are no initialization parameters.
 o getServletConfig()
Returns a servletConfig object containing any startup configuration information for this servlet.
 o getServletContext()
Returns a ServletContext object, which contains information about the network service in which the servlet is running.
 o getServletInfo()
Returns a string that contains information about the servlet, such as its author, version, and copyright.
 o init(ServletConfig)
Initializes the servlet and logs the initialization.
 o log(String)
Writes the class name of the servlet and the given message to the servlet log file.
 o service(ServletRequest, ServletResponse)
Carries out a single request from the client.

Constructors

 o GenericServlet
 public GenericServlet()
The default constructor does no work.

Methods

 o getServletContext
 public ServletContext getServletContext()
Returns a ServletContext object, which contains information about the network service in which the servlet is running. This is a convenience method; it gets the ServletContext object from the ServletConfig object. (The ServletConfig object was passed into and stored by the init method.)

 o getInitParameter
 public String getInitParameter(String name)
Returns a string containing the value of the named initialization parameter, or null if the requested parameter does not exist. Init parameters have a single string value; it is the responsibility of the servlet writer to interpret the string.

This is a convenience method; it gets the parameter's value from the ServletConfig object. (The ServletConfig object was passed into and stored by the init method.)

Parameters:
name - the name of the parameter whose value is requested
 o getInitParameterNames
 public Enumeration getInitParameterNames()
Returns the names of the servlet's initialization parameters as an enumeration of strings, or an empty enumeration if there are no initialization parameters. The getInitParameterNames method is supplied for convenience; it gets the parameter names from the ServletConfig object. (The ServletConfig object was passed into and stored by the init method.)

 o log
 public void log(String msg)
Writes the class name of the servlet and the given message to the servlet log file. The name of the servlet log file is server specific; it is normally an event log.

If a servlet will have multiple instances (for example, if the network service runs the servlet for multiple virtual hosts), the servlet writer should override this method. The specialized method should log an instance identifier, along with the requested message. The default message prefix, the class name of the servlet, does not allow the log entries of the instances to be distinguished from one another.

Parameters:
msg - the message string to be logged
 o getServletInfo
 public String getServletInfo()
Returns a string that contains information about the servlet, such as its author, version, and copyright. This method must be overridden in order to return this information. If it is not overridden, null is returned.

 o init
 public void init(ServletConfig config) throws ServletException
Initializes the servlet and logs the initialization. The init method is called once, automatically, by the network service each time it loads the servlet. It is guaranteed to finish before any service requests are accepted. On fatal initialization errors, an UnavailableException should be thrown. Do not call call the method System.exit.

The init method stores the ServletConfig object. Servlet writers who specialize this method should call either super.init, or store the ServletConfig object themselves. If an implementor decides to store the ServletConfig object in a different location, then the getServletConfig method must also be overridden.

Parameters:
config - servlet configuration information
Throws: ServletException
if a servlet exception has occurred
See Also:
UnavailableException
 o getServletConfig
 public ServletConfig getServletConfig()
Returns a servletConfig object containing any startup configuration information for this servlet.

 o service
 public abstract void service(ServletRequest req,
                              ServletResponse res) throws ServletException, IOException
Carries out a single request from the client. The request object contains parameters provided by the client, and an input stream, which can also bring data to the servlet. To return information to the client, write to the output stream of the response object.

Service requests handled after servlet initialization has completed. Any requests for service that are received during initialization block until it is complete.

Note that servlets typically run inside multi-threaded network services, which can handle multiple service requests simultaneously. It is the servlet writer's responsibility to synchronize access to any shared resources, such as database or network connections. The simplest way to do this is to synchronize the entire service call. This can have a major performance impact, however, and should be avoided whenever possible in favor of techniques that are less coarse. For more information on synchronization, see the the Java tutorial on multithreaded programming.

Parameters:
req - the servlet request
res - the servlet response
Throws: ServletException
if a servlet exception has occurred
Throws: IOException
if an I/O exception has occurred
 o destroy
 public void destroy()
Destroys the servlet, cleaning up whatever resources are being held, and logs the destruction in the servlet log file. This method is called, once, automatically, by the network service each time it removes the servlet. After destroy is run, it cannot be called again until the network service reloads the servlet.

When the network service removes a servlet, it calls destroy after all service calls have been completed, or a service-specific number of seconds have passed, whichever comes first. In the case of long-running operations, there could be other threads running service requests when destroy is called. The servlet writer is responsible for making sure that any threads still in the service method complete.


All Packages  Class Hierarchy  This Package  Previous  Next  Index