Java The Complete Reference, Seventh Edition

(Greg DeLong) #1
The ServletRequest Interface

TheServletRequestinterface enables a servlet toobtain information about a client request.
Several of its methods are summarized in Table 31-3.


The ServletResponse Interface

TheServletResponseinterface enables a servlet to formulate a response for a client. Several
of its methods are summarized in Table 31-4.


Chapter 31: Servlets 913


Method Description
Object getAttribute(Stringattr) Returns the value of the ser ver attribute namedattr.
String getMimeType(Stringfile) Returns the MIME type offile.
String getRealPath(Stringvpath) Returns the real path that corresponds to the virtual
pathvpath.
String getSer verInfo( ) Returns information about the ser ver.
void log(Strings) Writessto the ser vlet log.
void log(Strings, Throwablee) Writessand the stack trace foreto the ser vlet log.
void setAttribute(Stringattr, Objectval) Sets the attribute specified byattrto the value
passed inval.

TABLE 31-2 Various Methods Defined byServletContext


Method Description
void destroy( ) Called when the ser vlet is unloaded.
Ser vletConfig getSer vletConfig( ) Returns aServletConfigobject that contains any initialization
parameters.
String getSer vletInfo( ) Returns a string describing the ser vlet.
void init(Ser vletConfigsc)
throws Ser vletException

Called when the ser vlet is initialized. Initialization
parameters for the ser vlet can be obtained fromsc.
AnUnavailableExceptionshould be thrown if the
ser vlet cannot be initialized.
void ser vice(Ser vletRequestreq,
Ser vletResponseres)
throws Ser vletException,
IOException

Called to process a request from a client. The request from
the client can be read fromreq.The response to the client
can be written tores.An exception is generated if a ser vlet
or IO problem occurs.

TABLE 31-1 The Methods Defined byServlet

Free download pdf