|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--javax.servlet.GenericServlet
Defines a generic, protocol-independent
servlet. To write an HTTP servlet to use with
a Web site, you must extend HttpServlet
.
GenericServlet
implements the Servlet
and ServletConfig
interfaces. When you write a servlet,
you usually extend GenericServlet
or its subclass
HttpServlet
, unless the servlet needs another superclass.
If a servlet needs to extend a class other than GenericServlet
or HttpServlet
, the servlet must implement the Servlet
interface directly.
GenericServlet
makes writing servlets
easier. It provides simple versions of the lifecycle methods
init
and destroy
and of the methods
in the ServletConfig
interface. GenericServlet
also implements the log>
method, declared in the
ServletContext
interface.
To write a generic servlet, you need only
override the service
method, which is declared as
an abstract method with no body. If you are writing a servlet
engine, you should override getServletInfo
and specialize
the init
and destroy
methods if
the engine will manage expensive servlet-wide
resources.
Constructor Summary | |
GenericServlet()
Does nothing, because this is an abstract class. |
Method Summary | |
void |
destroy()
Destroys the servlet, cleaning up whatever resources are being held. |
java.lang.String |
getInitParameter(java.lang.String name)
Returns a String containing the value of the named
initialization parameter. |
java.util.Enumeration |
getInitParameterNames()
Returns the names of the initialization parameters for this servlet as an enumeration of String objects. |
ServletConfig |
getServletConfig()
Returns a ServletConfig object, which gives
a servlet its initialization parameters. |
ServletContext |
getServletContext()
Returns a ServletContext object, which contains
information about the servlet engine on 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 information. |
void |
init()
Acts as a convenience method, so that you do not have to store a ServletConfig object to use as a
parameter. |
void |
init(ServletConfig config)
Initializes this servlet. |
void |
log(java.lang.String msg)
Writes the servlet class name and a servlet exception message to the servlet log file. |
void |
log(java.lang.String message,
java.lang.Throwable t)
Writes a system exception message to the servlet log file. |
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()
init
methods.Method Detail |
public void destroy()
The servlet engine calls this method once,
automatically, when it removes the servlet. After the engine
calls destroy
, it cannot call destroy
again on this instance of the servlet.
The engine calls destroy
after all
calls to the service
method have completed
or a specified amount of time has passed, whichever occurs
first. In the latter case, the service
method might stil be servicing requests from other threads.
When you write your servlet, make sure that any
threads still running in the service
method
complete before the servlet is destroyed.
public java.lang.String getInitParameter(java.lang.String name)
String
containing the value of the named
initialization parameter. If the servlet does not have
a parameter of the specified name, this method returns
null
.
An initialization parameter has a single String
value, which you must interpret.
This method is supplied for convenience. It gets the
value of the named parameter from the ServletConfig
object, which is passed
to the servlet by the init
method.
name
- a String
specifying the name
of the initialization parameterString
containing the value
of the initalization parameterpublic java.util.Enumeration getInitParameterNames()
String
objects.
If the servlet has no initialization paramaters, this method returns an empty enumeration.
This method is supplied for convenience. It gets the
parameter names from the ServletConfig
object,
which the init
method passes to the servlet.
String
objects containing the names of
the servlet's initialization parameterspublic ServletConfig getServletConfig()
ServletConfig
object, which gives
a servlet its initialization parameters. The initialization
parameters supply the initial or default values the
servlet runs with.ServletConfig
object
that initialized this servletpublic ServletContext getServletContext()
ServletContext
object, which contains
information about the servlet engine on which the servlet
is running.
This method is supplied for convenience. The
ServletContext
object is contained within the
ServletConfig
object, which is passed to the
servlet by the init
method when the servlet is
initialized.
ServletContext
object
passed to this servlet by the init
methodpublic java.lang.String getServletInfo()
String
that contains information about
the servlet such as its author, version, and copyright information.
You must override this method before it returns this information.
If you do not override this method, it returns an empty string.String
until
you override this methodpublic void init(ServletConfig config) throws ServletException
The servlet engine calls this method once,
automatically, each time it loads the servlet. This
method is guaranteed to finish before the servlet accepts any
requests to its service
method. If a fatal
error occurs while the servlet is being initialized,
the servlet engine should throw
an UnavailableException
, rather than
calling the System.exit
method.
The init
method stores the
ServletConfig
object it receives from the servlet engine. If you override init
,
you should either call super.init
or store the ServletConfig
object in the new
init
method. If you decide to store the
ServletConfig
object in a different location,
you should also override the getServletConfig()
method.
config
- the ServletConfig
object
that contains initialization parameters
for this servletUnavailableException
public void init() throws ServletException
ServletConfig
object to use as a
parameter.
If you extend GenericServlet
, simply override
this method and it will be called by
GenericServlet.init(ServletConfig config)
.
public void log(java.lang.String msg)
The default message prefix, which is the servlet class name, does not allow the servlet log entries to be distinguished from one another.
The servlet log file is an event log file whose name is specific to the server.
msg
- a String
specifying
a servlet exception messagepublic void log(java.lang.String message, java.lang.Throwable t)
message
- a String
containing a
description of a system exceptiont
- an exception of type
java.lang.Throwable
public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException
Requests sent to this method are handled after servlet initialization is complete. If any requests are received while the servlet is being initializaed, they are blocked.
The ServletRequest
object the client passes
to this method contains parameters the client provides, as
well as an input stream that gives the servlet data.
The ServletResponse
object contains an output
stream that the servlet can use to return information to the
client.
Servlets typically run inside multithreaded servlet engines,
which can handle multiple service
requests concurrently.
Therefore, you must 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. For more information on synchronization,
see the
Java tutorial on multithreaded programming.
req
- the ServletRequest
object
that contains the client's requestres
- the ServletResponse
object
that will contain the servlet's response
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |