This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JSP and the fly likes Request dispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Request dispatcher" Watch "Request dispatcher" New topic
Author

Request dispatcher

rajaji raja
Greenhorn

Joined: Jul 27, 2003
Posts: 9
what is the difference between the RequestDispatcher's forward and
sendRedirect and forward action
amol re
Ranch Hand

Joined: Mar 21, 2002
Posts: 36
forward action in your JSP is converted into a RequestDispatcher.forward() in the translated servlet.
sendRedirect and RequestDispatcher.forward() are different in the following ways:
  • Send redirect sends the response to the client with redirect header(i dont know the exact name) which has information about which url to resend request to, to get the actual response. So it involves a round trip. client request->send redicret response->client resends request->response.
  • Forward, on the other hand, handles the forwarding of the request entirely on the server side. The client doesnt even come to know that the reponse was generated by some resource other than the one it had asked the response from.
  • So, the URL in the browser's address bar changes in case of sendRedirect but doesnt change in case of forward.
  • Since sendRedirect forces the client to send a new request to the specified url, you cannot share request scope attributes between servlets if you are using sendRedirect. In case of foward, the request object remains the same which allows u to set some attributes in the request before forwarding it and retrieve them in the forwarded servlet/jsp.

  • Bob Kerfoot
    Ranch Hand

    Joined: Oct 01, 2000
    Posts: 47
    The RequestDispatcher's forward method and the jsp:forward action are both server-centric. Specifically, your request is forwarded to the target resource on the server without notifying the client first. Thus, a single request requires only a single round trip between client and server.
    In contrast, the sendRedirect method sends a message back to the client instructing it to request the target resource. Thus, a single request requires two round trips between the client and server to get the final target resource.
    Bob Kerfoot
     
    wood burning stoves
     
    subject: Request dispatcher