background image

Invoking OtherWeb Resources

<< Filter-to-Servlet Mapping | HttpServlet Class >>
<< Filter-to-Servlet Mapping | HttpServlet Class >>

Invoking OtherWeb Resources

chain by means of the chain.doFilter method. Because S1's filter chain contains filters F1 and
F3, F1's call to chain.doFilter invokes the doFilter method of filter F3. When F3's doFilter
method completes, control returns to F1's doFilter method.
Invoking Other Web Resources
Web components can invoke other web resources in two ways: indirectly and directly. A web
component indirectly invokes another web resource when it embeds a URL that points to
another web component in content returned to a client. In the Duke's Bookstore application,
most web components contain embedded URLs that point to other web components. For
example, ShowCartServlet indirectly invokes the CatalogServlet through the following
embedded URL:
/bookstore1/catalog
A web component can also directly invoke another resource while it is executing. There are two
possibilities: The web component can include the content of another resource, or it can forward
a request to another resource.
To invoke a resource available on the server that is running a web component, you must first
obtain a
RequestDispatcher
object using the getRequestDispatcher("URL") method.
You can get a RequestDispatcher object from either a request or the web context; however, the
two methods have slightly different behavior. The method takes the path to the requested
resource as an argument. A request can take a relative path (that is, one that does not begin with
a /), but the web context requires an absolute path. If the resource is not available or if the server
has not implemented a RequestDispatcher object for that type of resource,
getRequestDispatcher
will return null. Your servlet should be prepared to deal with this
condition.
Including Other Resources in the Response
It is often useful to include another web resource (for example, banner content or copyright
information) in the response returned from a web component. To include another resource,
invoke the include method of a RequestDispatcher object:
include(request, response);
Invoking Other Web Resources
The Java EE 5 Tutorial · September 2007
122