Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Diff Between redirect & forward

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the difference between response.redirect() & getRequestDispatcher.forward();
Can someone help me out by giving a example where we can use one but not the other.
Thnx.
 
TV KUMAR
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
response.sendRedirect() method is used to just redirect the request. By this method the request variables will not be available in the forwarded page. Where as for request despatching they will be available.
Suppose u have a HTML text field by the name 'name'.Suppose the form's action page is a servlet 'A'. After the form is submitted, in servlet 'A', u can access the value entered in the text field by using request.getParameter("name"). Suppose u have redirected the request from 'A' to servlet 'B'. In 'B' u cannot access the parameter "name" as u did in servlet 'A'. U can access it only if u had despatched it.
When u use sendRedirect(), the response is committed to the browser and it is the browser which calls the second servlet.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it has any thing to do with the Application context.If no, then
What if i want to forward or redirect some request to a jsp/servlet of another context.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Nirant",
We're pleased to have you here with us in the JSP forum, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
Thanks!
bear
Forum Bartender
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TV KUMAR,
JavaRanch is a community of people from all over the world, many of who are not native English speakers. While using abbreviations like "u" instead of spelling out "you" is convenient when text messaging your friends on a cell phone or in a chat room, it presents an extra challenge to those that are already struggling with English. Additionally, such shortcuts may confound automated translation tools that patrons of the Ranch may be making use of.
I would like to ask for your help in making the content of JavaRanch a little easier to read for everybody that visits here by not using such abbreviations.
thanks,
bear
JSP Forum Bartender
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic