Meaningless Drivel is fun!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Comparision between redirect and forward Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Comparision between redirect and forward" Watch "Comparision between redirect and forward" New topic
Author

Comparision between redirect and forward

Sreenath Madasu
Ranch Hand

Joined: Mar 26, 2002
Posts: 32
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?

Thanks
Sreenath Madasu


SCJP1.2<br />SCWCD1.4<br />SCBCD1.3
Mikalai Zaikin
Ranch Hand

Joined: Jun 04, 2002
Posts: 3210
    
    5
Hi Sreenath,

1. HttpServletResponse.sendRedirect(...)

- 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

[more info]

2. RequestDispatcher.forward(...)

- 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

[more details]

regards,
MZ


Free SCDJWS 5.0 Study Guide - SCDJWS 5.0 Quiz (How to get SCDJWS 5.0 Quiz)
Java Platform, Enterprise Edition 6 Web Services Developer Certified Expert Exam Study Guide and Quiz
Mikalai Zaikin
Ranch Hand

Joined: Jun 04, 2002
Posts: 3210
    
    5
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'

regards,
MZ
Sreenath Madasu
Ranch Hand

Joined: Mar 26, 2002
Posts: 32
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparision between redirect and forward