This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes servlet to servlet communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet to servlet communication" Watch "servlet to servlet communication" New topic
Author

servlet to servlet communication

sonali Kapoor
Greenhorn

Joined: Sep 03, 2001
Posts: 2
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
BJ Grau
Ranch Hand

Joined: Jul 10, 2001
Posts: 234
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

Joined: Sep 03, 2001
Posts: 2
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).]

Rishi Singh
Ranch Hand

Joined: Dec 09, 2000
Posts: 321
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
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

(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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet to servlet communication
 
Similar Threads
HttpServletRequest's getQueryString()
HTTP Status 405 error comes
Form Variables
Doubt on servlet request lifecycle
Issue with request.setAttribute(String,obj)