File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes sendredirect/forward(req,res) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendredirect/forward(req,res)" Watch "sendredirect/forward(req,res)" New topic


kajal sharma

Joined: Oct 04, 2000
Posts: 22
Dear java guru's
I want to know what is the diffrence between using
getServletConfig().getServletContext().getRequestDispatcher("/report/wait.jsp").forward(req, res);
kajal sharma
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
getServletConfig().getServletContext().getRequestDispatcher("/report/wait.jsp").forward(req, res);
This tells the servlet engine to have the request handled by the resource (jsp, in this case) "/report/wait.jsp", in the same web application, with the location specified relative to the web application's root. The forward is completely internal to the server; neither the client nor its user ever knows (or needs to know) that the actual HTTP response is coming from somewhere else. You can find a more detailed description in section 8 of the servlet spec, v2.2, available from Sun.
You would use a forward if you split your application up in small chunks, probably in pursuit of a clear division of responsibilities. The most obvious example is the MVC (Model/View/Controller) architecture, where the Controller servlet receives the client request, updates the Model, then forwards the request to the View which is a JSP responsible for displaying the result to the client.
This does something completely different. It sets the HTTP response headers to 307 Temporary Redirect, and commits the response to the client browser (see section 6.3 of the servlet spec, v2.2, and also the HTTP 1.1 spec). The browser then reacts by retrieving the new URL, and also displays the URL to the user in the Address window. It is interpreted as any other URL, in your example it is relative to the root of the site. In other words, if the root of your web-application is something other than "/", your example forward and your redirect will refer to two different locations.
You would use this if you want the user to be aware that (s)he is going to a different location, or if you need to go to another application on the same server, or maybe even a different server anywhere in the world.
- Peter

[This message has been edited by Peter den Haan (edited May 05, 2001).]
I agree. Here's the link:
subject: sendredirect/forward(req,res)
jQuery in Action, 3rd edition