|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--javax.servlet.GenericServlet
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 life-cycle 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 Summary | |
GenericServlet()
The default constructor does no work. |
Method Summary | |
void |
destroy()
Destroys the servlet, cleaning up whatever resources are being held, and logs the destruction in the servlet log file. |
java.lang.String |
getInitParameter(java.lang.String name)
Returns a string containing the value of the named initialization parameter, or null if the requested parameter does not exist. |
java.util.Enumeration |
getInitParameterNames()
Returns the names of the initialization parameters for this servlet as an enumeration of Strings, or an empty enumeration if there are no initialization parameters. |
ServletConfig |
getServletConfig()
Returns a servletConfig object containing any startup configuration information for this servlet. |
ServletContext |
getServletContext()
Returns a ServletContext object, which contains information about the network service in which the servlet is running. |
java.lang.String |
getServletInfo()
Returns a string that contains information about the servlet, such as its author, version, and copyright. |
void |
init()
This method is provided as a convenience so that servlet writers do not have to worry about storing the ServletConfig object. |
void |
init(ServletConfig config)
Initializes the servlet and logs the initialization. |
void |
log(java.lang.String msg)
Writes the class name of the servlet and the given message to the servlet log file. |
void |
log(java.lang.String message,
java.lang.Throwable t)
Logs the message with the root cause |
abstract void |
service(ServletRequest req,
ServletResponse res)
Carries out a single request from the client. |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Constructor Detail |
public GenericServlet()
Method Detail |
public void destroy()
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.
public java.lang.String getInitParameter(java.lang.String name)
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.)
name
- the name of the parameter whose value is requestedpublic java.util.Enumeration getInitParameterNames()
This 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.)
public ServletConfig getServletConfig()
public ServletContext getServletContext()
public java.lang.String getServletInfo()
public void init(ServletConfig config) throws ServletException
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.
config
- servlet configuration informationUnavailableException
public void init() throws ServletException
public void log(java.lang.String msg)
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 and possibly a thread 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.
msg
- the message string to be loggedpublic void log(java.lang.String message, java.lang.Throwable t)
public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException
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.
req
- the servlet requestres
- the servlet response
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |