This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

servlet to servlet communication

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I have some problem in sending parameters from one servlet to another.
I have a HTML page which sends some data using
method=POST
to "servlet1".
In "servlet1" I am redirecting the servlet to another servlet
by appending the values to the target URL say
response.sendRedirect("www.foo.com?"+parametersString);
It is sending the values by default by GET method.
Is there a way to redirect these values by POST method so that the values sent to the second servlet are not visible in the browser's address (URL).
Thanks in advance
Sonali
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try RequestDispatcher.forward(ServletRequest, ServletResponse)
I think you need to make sure you haven't written anything out to the client before you call this, otherwise you'll get an error.
[This message has been edited by BJ Grau (edited September 03, 2001).]
 
sonali Kapoor
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Thanks for the reply. I tried as given by u.I am not writing on to the response object. Using forward is invoking the second servlet but the parmeters of "servlet1" are not being passed to the "servlet2".
I forgot to tell you on thing. The html page from which I am calling "servlet1" is uploading a file with ENCTYPE="multipart/form-data".
I am using MultipartRequest to upload the file in the "servlet1".Thank you once again.
Sonali

Originally posted by BJ Grau:
Try RequestDispatcher.forward(ServletRequest, ServletResponse)
I think you need to make sure you haven't written anything out to the client before you call this, otherwise you'll get an error.
[This message has been edited by BJ Grau (edited September 03, 2001).]


 
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear sonali,
just try using the request.sendRedirect(String URL)method and pass the desired values to the 2nd servlet.I hope this should work
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(in my opinion etc)
In general you should not use response.sendRedirect() to communicate directly between two servlets since this results in two sets of HTTP message being sent.
The client sends a request to servlet1, servlet1 decides not to handle the request and executes a sendRedierct("servlet2").
This sends a message back to the client saying "don't look to servlet1 one, look to servlet2 for the request (response code 302, moved temporarily)
The client changes the url it diplays to "servlet2" and sends another requst for the server for servlet2.
The correct way is to use the RequestDispatcher, which allows servlets to communicate within he servlet and share the same request and response objects.
In this manner, servlet1 can decide to forward to servlet2, but can place some data on the request using setAttribute() to pass onto servlet2...
If you look at the RequestDispatcher, there are two ways to get from one servlet (or jsp) to another, include and forward.
include allows the data to be concatenated so that the page returned t the client can be built from several sources, forward attempts to ignore the initial servlet output and require the next to provide the output.
In general I prefer forward since the only effect servlet1 can have on the output is based on the data it places on the request. If it has attempted to set header data or write a HTML page this data is lost. (reduces confusion in servlet2 as to what or how much has already been done)

Dave.
 
These are the worst of times and these are the best of times. And this is the best tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!