Friday, October 07, 2005

Difference between response.sendRedirect and requestDispatcher.forward with respect to session?


When you forward the request with requestDispatcher.forward the target servlet/JSP share the same session. But with sendRedirect the session information is not preserved. ( target is not in scope of session)

A RequestDispatcher forward is a server side activity. So the request and its associated session are available to the forwarded resource.

If you use sendRedirect to redirect the user to a different context, i.e. a different application, of course the session information is not preserved. It would be meaningless because sessions are tied to a specific context. (for example, my session information from javaranch would have no meaning if I decided to go to yahoo)

However, using sendRedirect within the same context works perfectly.

You might have a scenario in which you want to redirect the request to a different URL, but still want to use a session. There is a special URL encoding method just for that:

response.encodeRedirectURL("/resourceName");

Remember to include the slash in the method.

3 Comments:

Blogger Rosa Benito said...

Technorati-Edelman Blog Survey Results Published
Things will be a bit quiet here until the weekend as I'm on the road. Meanwhile, take a look at the results of the Technorati/Edelman blog survey that was conducted late last month.
Find out how to buy and sell anything, like things related to music on interest free credit and pay back whenever you want! Exchange FREE ads on any topic, like music!

3:19 AM  
Blogger Roulette System Niche Online Marketing Guy said...

nPost.com Interview with David Geller of WhatCounts.com
David Geller, CEO of WhatCounts.com David Geller has quite a lot of experience in the email space.
Hey, you have a great blog here! I'm definitely going to bookmark you!

I have a gambling roulette roulette roulette syst site/blog. It pretty much covers gambling roulette roulette roulette syst related stuff.

Come and check it out if you get time :-)

3:19 AM  
Blogger ChrisJoel said...

Hey, you have an impressive blog here! I'm surely going to bookmark you! I have a golf package site. It pretty much covers golf package related stuff.

Come and check it out when you get time :-)

9:43 PM  

Post a Comment

<< Home