Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing variables in response.sendRedirect

 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to redirect my users to a page while at the same time passing one variable.

but response.sendRedirect needs a hardcoded value to pass and can't pass a dynamic one.

what it currently does is:
response.sendRedirect("sample.jsp?temp=1");

now, instead of the value "1", i want to pass a variable
 
Peter Sin
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shuini Gustav:
I need to redirect my users to a page while at the same time passing one variable.

but response.sendRedirect needs a hardcoded value to pass and can't pass a dynamic one.

what it currently does is:
response.sendRedirect("sample.jsp?temp=1");

now, instead of the value "1", i want to pass a variable


1) create strTemp variable
2) assign "1" to strTemp
3) rewrite response.sendRedirect(??? + ???)

Explore by yourself.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't want to use forward() ?
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried assigning a value to a variable and passing the variable in response.sendRedirect. it doesn't work. someone told me it needed a hard coded value to be able to pass something, and not a variable.

i didn't know variables can be passed in forward....
how is that done?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you forward the request, you can use setAttribute to put your variable in the request scope, which can easily be retrieved in your JSP.

 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a StringBuffer to dynamically append value like



Hope this helps.
[ March 07, 2006: Message edited by: ak pillai ]
 
Jherald Lacambra
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow long codes..

i only use this and its working fine:

String id = "123";
response.sendRedirect("toNexturl.jsp?id="+id);
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jherald Lacambra:
Wow long codes..

i only use this and its working fine:

String id = "123";
response.sendRedirect("toNexturl.jsp?id="+id);


this doesn't work
it outputs null
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satou kurinosuke:
If you forward the request, you can use setAttribute to put your variable in the request scope, which can easily be retrieved in your JSP.



what's the RequestDispatcher for?
 
Peter Sin
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shuini Gustav:


this doesn't work
it outputs null


How do you retrieve the value ?
[ March 07, 2006: Message edited by: Peter Sin ]
 
Miriam van Soest
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am not mistaken:

- RequestDispatcher + forward: You want your server to forward the request to another page/servlet etc...
- SendRedirect: You send an answer back to the client, which will tell the browser to go to another place.
 
Jherald Lacambra
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shuini Gustav:


this doesn't work
it outputs null


to check this if it is working
try to see the url

of course it should be http://mywebsite.com/toNextUrl.jsp?id=theValueOfId

this working. if not then the url set maybe like this http://mywebsite.com/toNextUrl.jsp?id=

so it will get null
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shuini Gustav:

this doesn't work
it outputs null


It will work just fine. You must be retrieving the value incorrectly.
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can it also pass more than one variable?
 
Jherald Lacambra
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shuini Gustav:
can it also pass more than one variable?


of course it can

response.sendRedirect("urNextUrl.jsp?fisrtVar="+var1+"&secondVar="+var2);
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can the response.sendRedirect be targeted to open in a new window?
if so, how's that done?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the response cannot be targetted to a differnent window. You need to target the request to the window in which you want the response to appear.
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. i want to target the response to a window I want it to appear in. much like the html attribute target="_blank".

how is that incorporated in response.sendRedirect?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I repeat: the response cannot be targetted to a different window. You'll need to use the HTML target attribute for the original request.
 
Jherald Lacambra
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want to do that you will have 2 windows and 2 redirects.
one will redirect a window that will open you page and then it will redirect again to the original page.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doing as Bear says is easier. Setting the target in the page calling the servlet. The page making the request should deal with this.
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic