• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in sendRedirect()  RSS feed

 
Senthil Kumar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Consider i have two servlets(serv1 and serv2) and a JSP.
Initially the request from JSP to serv1 and in serv1,i have a code as
response.sendRedirect("/serv2.do");
In this case will i b able to access the form parameters in serv2?
Since the request to serv2 goes as new request from the client,wont i b able to access the Form Fields values?
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try a simple application that illustrates that ?
 
Senthil Kumar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i hav tried it in an application.but am not able to access the request parameters.
But my doubt is since the request goes as a new one to the serv2 then y am not able to access the request Parameters(Form Field values)???
 
Bosun Bello
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Senthil, you are correct. A new request is initiated with sendRedirect. Why don't you just forward the request?
 
Varun Narang
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,
Since redirection of the request creates a new request and sends it to the path you specify in the sendRedirect method, you'll not be able to retrieve the request/form-field values in the next servlet. So, before you redirect you need to fetch all the necessary information you want to save and put it in session or application scope (setting session/application attributes may help), based on the requirement.
You can try putting the required values in a Map or something like that and store it in a session/application object by calling setAttribute() methods on them.

Hope this will help.

Regards,
VaruNarang.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The sendRedirect method returns a 302 (moved temporarily) response code to the browser along with a "Location" header containing the URL for the page to which you want to redirect.

If your form's action attribute was 'get' then the form parameters would be appended to the URL as a querystring and, yes, they would be available to the next page. If, on the other hand, the action was 'post', then they would not.

There are some alternatives.
If both pages are part of the same application, you might want to look into RequestDispatcher.forward. With this method, the move to the other page takes place entirely on the server. The same HttpServletRequest object is passed to the next page and your parameters will be available.
 
Senthil Kumar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for all of your replies.....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!