Java Reference
In-Depth Information
object for a servlet context, using it as a wrapper around a server resource
located at a particular address. The initial configuration for the Web application,
provided by the server when the servlet is initialized, supplies a servlet
context , which defines the scope of the Web application within the server.
A ServletContext object defines the set of methods that a servlet uses to
communicate with its servlet container.
As shown in lines 195 through 199, because the servlet extends HttpServlet,
the servlet can get a ServletContext object by calling the HttpServlet method,
getServletContext() , shown in line 196. The ServletContext method,
getRequestDispatcher() , then is called to obtain a RequestDispatcher object.
This method requires a path to the resource including the resource name, which
must begin with a slash (/) and is relative to the current context root. The
resource name is obtained from the corresponding session attribute in line 197.
This value, along with the slash, provides the required path passed to the
getRequestDispatcher() method.
Line 198 calls the forward() method of the RequestDispatcher object.
The RequestDispatcher forward() method forwards a request from a servlet
to another resource (servlet, JSP file, or HTML file) on the server. Because the pro-
cess occurs on the server side rather than the client side, forwarding a request is
more efficient than redirecting, which requires additional communication between
the client and server. Forwarding also transfers the request and response objects
originally received, rather than forcing the client to generate a new request.
The following steps enter the code to process the requested action and
forward the HTTP request.
To Forward an HTTP Request
1. Click the Save button
on the Standard
toolbar. Enter lines
110 through 150
as shown in
Figure 12-26 on page
809.
TextPad displays code
that gets the WSaction
parameter, calls appro-
priate methods, and
sets attributes, includ-
ing one for the receiv-
ing resource name
(Figure 12-27).
getParameter()
method obtains
form parameter
action taken,
depending
on value
of form
parameter
receiving
resource
name
FIGURE 12-27
(continued)
 
Search WWH ::




Custom Search