This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes difference b/w forward & sendRedirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "difference b/w forward & sendRedirect" Watch "difference b/w forward & sendRedirect" New topic
Author

difference b/w forward & sendRedirect

tilak kumar
Greenhorn

Joined: Feb 16, 2005
Posts: 15
What is the difference between RequestDispatcher.forward(request req,response res)
And response.sencRedirect("url");?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Send redirect sends a "location" header to the browser.
The browser then initiates a new request.
  • Browser requests page A
  • Server says go look for B
  • Browser requests B
  • Server returns page B

  • The browser knows that it is showing page B.
    The address window will have the URL for page B.


    With forward, the whole process occurs on the server.
  • Browser requests page A
  • Server returns page B

  • As far as the browser knows, its looking at page A.
    The address window in the browser will show the url
    for page A.

    Forward should be a little faster because it doesn't involve the extra round trip (small difference, a redirect only returns the headers). It's useful in an MVC pattern when you want to forward to a JSP (or other web resource) that you wouldn't want the user trying to access directly.

    There are other advantages and disadvantages to both.
    [ March 22, 2005: Message edited by: Ben Souther ]

    Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
    Poobhathy Kannan
    Ranch Hand

    Joined: May 26, 2004
    Posts: 94
    In addition to ben

    Redirects can redirect the request off of the local server as well, but forward works only on local server.


    http://learnertobeginner.blogspot.com/
    kri shan
    Ranch Hand

    Joined: Apr 08, 2004
    Posts: 1371
    Using RequestDispatcher.forward(request req,response res), can i forward to antoher server (or) within the same server?
    Sarath Mohan
    Ranch Hand

    Joined: Mar 17, 2001
    Posts: 213
    Hi Kri,

    You can forwad only to resource available in your current web application.


    Sarath Mohan
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: difference b/w forward & sendRedirect
     
    Similar Threads
    Array Initilaize
    Clarification
    interview question
    WA #1.....word association
    Why anonymous posts are allowed in this forum ?