File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes passing variables in response.sendRedirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "passing variables in response.sendRedirect" Watch "passing variables in response.sendRedirect" New topic
Author

passing variables in response.sendRedirect

Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
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

Joined: Jan 13, 2005
Posts: 547
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

You don't want to use forward() ?


[My Blog]
All roads lead to JavaRanch
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Feb 11, 2006
Posts: 288
Use a StringBuffer to dynamically append value like



Hope this helps.
[ March 07, 2006: Message edited by: ak pillai ]

java j2ee job interview questions with answers | Learn the core concepts and the key areas
Jherald Lacambra
Ranch Hand

Joined: Feb 02, 2005
Posts: 129
Wow long codes..

i only use this and its working fine:

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


jherald
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
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

Joined: Dec 20, 2005
Posts: 170
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

Joined: Jan 13, 2005
Posts: 547
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

Joined: Dec 21, 2005
Posts: 4
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

Joined: Feb 02, 2005
Posts: 129
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

Joined: Jan 10, 2002
Posts: 61187
    
  66

Originally posted by Shuini Gustav:

this doesn't work
it outputs null


It will work just fine. You must be retrieving the value incorrectly.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
can it also pass more than one variable?
Jherald Lacambra
Ranch Hand

Joined: Feb 02, 2005
Posts: 129
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

Joined: Dec 20, 2005
Posts: 170
thanks
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
can the response.sendRedirect be targeted to open in a new window?
if so, how's that done?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

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

Joined: Dec 20, 2005
Posts: 170
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

Joined: Jan 10, 2002
Posts: 61187
    
  66

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

Joined: Feb 02, 2005
Posts: 129
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Dec 20, 2005
Posts: 170
thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: passing variables in response.sendRedirect