Hi y'all! I just started preparing for SCWCD1.4 with one of the nicest and enjoyable books, HFS&J. When I was going through the chapter of redirecting request, I had a doubt. You guys have given comparion between redirect and forward. But when do we use redirect and when do we use forward? What are adv/disadv of each? Could you please clarify?
- sends response to client with redirect headers - can transfer client on any other web application - not transparent for client (redirection happens in 2 steps) - user sees in browser's address field the URL which application redirects to
- forwards request and response objects internally in Servlet Container to other resourse - safe to pass to next servlet any object as request attribute - can transfer client only within the same web application (servlet context) - transparent for the client (redirection happens in 1 step) - user sees original URL in address field, but output produced by another servlet/JSP
Another difference is that 'forward' allows to invoke servlets which are not accessible for user: 1) some password protected resource without using password 2) some resourse in /WEB-INF directory, which also is not served to user
You can not do this using 'sendRedirect'
Joined: Mar 26, 2002
Thanks. That means it is better to use forward most of the time if we are in the same app, since this happens in one step and user cannot see and book mark the URL.