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

response.redirect and jsp:forward

amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231
What is the difference between response.sendRedirect and jsp:forward? Which one is better?
Jason Brawner
Ranch Hand

Joined: Dec 19, 2001
Posts: 66
If you'd like to be sent to another page within your web application use jsp:forward. Use send redirect if you want to direct the user to an external site.
Hope this helps,

Jason E. Brawner<br />Sun Certified Java Programmer<br />Sun Certified Web Component Developer
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

I also dropped another explaination here. THis is a very common question so you may want to try the search functionality.
Biju Nair
Ranch Hand

Joined: Dec 15, 2001
Posts: 31
Both <jsp:forward> and response.sendRedirect is used to send the user to a JSP page or servlet or web page that's different from what the user had initially requested. In simple words, if u want to do some processing of user given parametres before u want to direct user to a page, u can use them. For e.g: while validating user through login page, u can check the validity of user and if a valid user u can redirect him to one page and if not valid u can redirect him to an error page or something.
Now when to use <jsp:forward> and when to use response.sendRedirect() ?
To a programmer, the major difference is :
1. The <jsp:forward> tag forwards the HttpServletRequest object with all the HTTP parameters to the destination page. With the response.sendRedirect() method, all HTTP parameter information is lost. ie say you had passed the username and password from the login.jsp page to login_action.jsp. In login_action.jsp, u validate the user redirect to customizedpage.jsp. If you use <jsp:forward> to redirect to customizedpage.jsp, then u will be able to get the value of username and password using request.getParameter("") in that page. If u are using response.sendRedirect, u will not get the value.
2. The response.sendRedirect() method sends the redirect HTTP code 301 back to the browser with the new URL. This forces the browser to send another request to the server. Therefore, response.sendRedirect() is much less efficient. ie A separate request is made to the server in the case of response.sendRedirect.
So in short use response.sendRedirect if the original jsp page is unnecessary and u don't want to retain the HTTP parametres. Use <jsp:forward> if u want to.
Also make sure u use them before any output statements or blocks. Else depending on how your JSP container buffers output, you may get an exception that the request can't be forwarded or redirected.

--: Biju Nair :--
I agree. Here's the link:
subject: response.redirect and jsp:forward
It's not a secret anymore!