File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » JSP
Bookmark "Request dispatcher" Watch "Request dispatcher" New topic

Request dispatcher

rajaji raja

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
    I agree. Here's the link:
    subject: Request dispatcher
    It's not a secret anymore!