Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Doubt in sendRedirect()

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)???
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Senthil, you are correct. A new request is initiated with sendRedirect. Why don't you just forward the request?
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for all of your replies.....
reply
    Bookmark Topic Watch Topic
  • New Topic