Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
376
Chapter 15 Integrating Servlets and JSP
pages, or even static HTML) can send output to the client, they should not
try to set HTTP response headers. Here is an example:
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("...");
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/path/resource");
dispatcher.include(request, response);
out.println("...");
The
include
method has many of the same features as the
forward
method. If the original method uses
POST
, so does the forwarded request.
Whatever request data was associated with the original request is also associ
ated with the auxiliary request, and you can add new parameters (in version
2.2 only) by appending them to the URL supplied to
getRequestDis
patcher
. Also supported in version 2.2 is the ability to get a
RequestDis
patcher
by name (
getNamedDispatcher
) or by using a relative URL (use
the
getRequestDispatcher
method of the
HttpServletRequest
); see Sec
tion 15.1 (Forwarding Requests) for details. However,
include
does one
thing that
forward
does not: it automatically sets up attributes in the
Http
ServletRequest
object that describe the original request path in case the
included servlet or JSP page needs that information. These attributes,
available to the included resource by calling
getAttribute
on the
Http
ServletRequest
, are listed below:
javax.servlet.include.request_uri
javax.servlet.include.context_path
javax.servlet.include.servlet_path
javax.servlet.include.path_info
javax.servlet.include.query_string
Note that this type of file inclusion is not the same as the nonstandard
servlet chaining supported as an extension by several early servlet engines.
With servlet chaining, each servlet in a series of requests can see (and mod
ify) the output of the servlet before it. With the
include
method of
RequestDispatcher
, the included resource cannot see the output generated
by the original servlet. In fact, there is no standard construct in the servlet
specification that reproduces the behavior of servlet chaining.
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved