22
What is the purposeWhat is the purpose
Forward a request from one servlet Forward a request from one servlet to another (or jsp).to another (or jsp).
Have first servlet do some of the Have first servlet do some of the work and then pass on to another.work and then pass on to another.
Can even forward on to a static Can even forward on to a static source like htmlsource like html
33
The Request DispatherThe Request Dispather
The The RequestDispatcherRequestDispatcher object is used to object is used to send a a client request to any send a a client request to any resource on the serverresource on the server
Such a resource may be dynamic Such a resource may be dynamic (e.g. a Servlet or a JSP file) or static (e.g. a Servlet or a JSP file) or static (e.g. a HTML document)(e.g. a HTML document)
To send a request to a resource To send a request to a resource xx, , use:use:
getServletContext().getRequestDispatcher("getServletContext().getRequestDispatcher("xx")")
44
Request Dispatcher MethodsRequest Dispatcher Methods
void forward(ServletRequest request, void forward(ServletRequest request,
ServletResponse response) ServletResponse response)
• Forwards a request from a Servlet to another Forwards a request from a Servlet to another
resource resource
void include(ServletRequest request, void include(ServletRequest request,
ServletResponse response) ServletResponse response)
• Includes the content of a resource in the current Includes the content of a resource in the current
responseresponse
55
Passing on DataPassing on Data 3 different ways to pass parameters for the 3 different ways to pass parameters for the
forwarded Servlet or JSPforwarded Servlet or JSP• Data that will be used only for this request:Data that will be used only for this request:
request.setAttribute("request.setAttribute("keykey", value);", value);
• Data will be used for this client (also for future Data will be used for this client (also for future requests):requests):
session.setAttribute("session.setAttribute("keykey", value);", value);
• Data that will be used in the future for every clientData that will be used in the future for every client
context.setAttribute("context.setAttribute("keykey", value);", value);
66
An ExampleAn Example The Servlet The Servlet JokesAndImagesJokesAndImages enables a user to enables a user to
choose a random joke or a random imagechoose a random joke or a random image The server has 5 images in the directory The server has 5 images in the directory images/images/
and five jokes (and five jokes (txttxt files) in the directory files) in the directory jokes/jokes/ Empty requests are forwarded to a HTML file that Empty requests are forwarded to a HTML file that
enables the user to choose a joke or an imageenables the user to choose a joke or an image Requests to a joke are forwarded to the servlet Requests to a joke are forwarded to the servlet
JokesJokes Requests to an image are forwarded to a random Requests to an image are forwarded to a random
image from the directory image from the directory images/images/
77
Jokes and ImagesJokes and Images<html>
<head><title>Images and Jokes</title></head>
<body>
<h1>Please Select:</h1>
<form method="post" action="JokesAndImages">
<h2>
<input type="submit" name="joke"
value="A Joke" />
<input type="submit" name="image"
value="An Image" />
</h2>
</form>
</body></html> imagesJokesOptions.html
88
Jokes and Images (cont)Jokes and Images (cont)public class JokesAndImages extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
int randomNum = 1 + Math.abs((new Random()).nextInt() % 5);
if (req.getParameter("joke") != null) {
req.setAttribute("jokeNumber", new Integer(randomNum));
getServletContext().getRequestDispatcher("/Jokes").forward(req,res);
} else if (req.getParameter("image") != null) {
getServletContext().getRequestDispatcher("/images/image" +
randomNum + ".gif").forward(req, res);
} else getServletContext().getRequestDispatcher
("/imagesJokesOptions.html"). forward(req,res);
}
public void doGet ... }} JokesAndImages.java
99
Jokes and Images (cont)Jokes and Images (cont)public class Jokes extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html><body><h1>A Joke</h1><pre>");
int jokeNum = ((Integer) req.getAttribute("jokeNumber")).intValue();
getServletContext().getRequestDispatcher
("/jokes/joke" + jokeNum + ".txt").include(req, res);
out.println("\n</pre>");
out.println("<a href=\"" + req.getRequestURL() + "\">Back</a>");
out.println("</body></html>");
}} Jokes.java
1010
Forwarding versus RedirectionForwarding versus Redirection
By default, By default, SendRedirectSendRedirect does not does not preserve parameters of the requestpreserve parameters of the request
SendRedirectSendRedirect ends up with a different ends up with a different URL on the clientURL on the client