• Post Reply Bookmark Topic Watch Topic
  • New Topic

Redirect instead of forward  RSS feed

 
Ed Carrington
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Tomcat 4.1.27 servlet I am using RequestDispatcher type to forward a message and get it in JSP. How can I do the same using redirect instead?

Here is what I have -

Servlet:

toPage.jsp
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since redirecting involves having the browser create a brand new request, you can't use request scope to pass variables from one page to another this way.

You can use session scope or querystring variables.
 
Ed Carrington
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, would this be the right way using a Session variable to pass along with the redirect?

Servlet

toPage.jsp
[ June 08, 2008: Message edited by: Ed Carrington ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"The right way" depends on your app.
That would allow you to pass the variable from one page to another.

Now that your variable is bound to session scope, you'll need to manage its state yourself (ie: remove it if it could be used by another page in a different way).
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to pass just few short values, I'd recommend to use redirect request parameters. Using session can be not robust enough in case of concurrent requests in the same session scope.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by D Rog:
If you need to pass just few short values, I'd recommend to use redirect request parameters. Using session can be not robust enough in case of concurrent requests in the same session scope.


Do you mean query string parameters?
 
Zaheer A Baloch
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code shall do it...

String myMessage = "Hello";
// session.setAttribute("myMessage",myMessage);
response.sendRedirect("/toPage.jsp?myMessage=" + myMessage);




// Zaheer
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!