Mock tests, Interview questions, Tutorials and Tech news
 
 
Home > Programming / tutorials > Difference between RequestDispatcher forward method and HttpServletResponse sendRedirect method

Difference between RequestDispatcher forward method and HttpServletResponse sendRedirect method

resonpse.sendRedirect() will forward the request to a new location. You can see the new location in the url of the browser. resonpse.sendRedirect() will direct the request to new location.

for eg resonpse.sendRedirect(“http://www.yahoo.com”) will take you to www.yahoo.com and you can see this address change on your browser address bar.

Another example is in Skill-Guru.com If we do <%response.sendRedirect(“login/homepage.faces”);%> in index,jsp, the request will be directed a new page and the url will change to

http://www.skill-guru.com/skill/login/homepage.faces

The sendRedirect method of HttpServletResponse actually modifies response header and tells the browser (the client) that the requested page is relocated and header also contains the new location of the page Now let us see the how jsp:forward works .  If we change the code to do this

<jsp:forward page=”/login/homepage.faces”/>

the request will be forwarded to homepage but url would look like

http://www.skill-guru.com/skill/login/homepage.faces

Why does that happen ?

The forward() method of RequestDispatcher will forward the ServletRequest and ServletResponse that it is passed to the path that was specified in getRequestDispatcher(“/login/homepage.faces“). The response will not be sent back to the client and the web container internally redirects the request to the other JSP/Servlet. Remember , you can redirect only to a page within current servlet context. The client will not know about this change of resource on the server

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • IndianPad
  • Reddit
Categories: Programming / tutorials Tags: ,
  1. No comments yet.
  1. No trackbacks yet.
Get Adobe Flash playerPlugin by wpburn.com wordpress themes