wood burning stoves 2.0*
The moose likes Servlets and the fly likes Urgent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Urgent" Watch "Urgent" New topic
Author

Urgent

Ganapathi Srinivasan
Ranch Hand

Joined: Apr 18, 2001
Posts: 41
We want to give the request and response objects from one servlet (say Servlet A) to another (say Servlet B). But we do not want Servlet A to wait until Servlet B finishes this request. We want to Servlet A to dispatch this info to Servlet B and continue with its own processing something like an asynchronous call? Is this possible with RequestDispatcher.forward() ???
Also can we make forward()calls in a loop..
eg.
RequestDispatcher rd = new RequestDispatcher(/servlet/ServletB);
for(int i=0;i<100;i++)
{
request.setAttribute("A",A[i]);
rd.forward(request,response);
}

Can anyone please rep.
Thanks and Regards,
Ganp.
Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
Hi
why don't you use a thread in servletA to invoke the method rd.forward(request,response). Have servlet A implement Runnable
Then in servlet A:

This will generate 100 threads each invoking the forward method.
I am not sure whether you want the same request object to be sent in each forward call in the loop or not. I assume that you want to send the same request object that (you recieve in the doGet or doPost method) as argument in each forward call of the loop. However I also assume that for each such call only the attribute 'A' of the request will differ. So I obtain a clone of the request object sent for that doGet or doPost and simple add/modify the 'A' attribute before passing it thru the forward method.
If you don't like the cloning method another way is to put the

inside the run method and start a thread :<br>

hope this helps
regards
Tanveer

Author of JPhotoBrush Pro (www.jphotobrushpro.com)
Ganapathi Srinivasan
Ranch Hand

Joined: Apr 18, 2001
Posts: 41
Hi Tanveer...
Thnaks for the suggestion.
But there seems to be a problem. You had suggested a clone() of HttpServetRequest. But when we do it we get a compilation error that clone() is a protected method in java.lang.object
we are not sure of this clone() ka funda. Can u throw some more light into ur logic??
Thanks and Regards,
Ganapathi
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Ganapathi Raman:
We want to give the request and response objects from one servlet (say Servlet A) to another (say Servlet B). But we do not want Servlet A to wait until Servlet B finishes this request. We want to Servlet A to dispatch this info to Servlet B and continue with its own processing [...]

There seems to be something fundamentally wrong about what you're trying to do. HTTP Servlets are objects which generate HTTP responses in response to HTTP requests, or delegate the generation of such responses.
If there is some work which should be done after forwarding the request to bean B, this work has no part in generating the response. For that reason alone it does not belong in a servlet, but in an ordinary Java class. Why not create a Runnable class C which implements this work? Immediately before the forward, you could say new Thread(new C()).run() to kick off the background work (probably not exactly this, but you get my drift).
If servlet A gets a lot of load, consider using an application-bound thread pool consuming Runnable jobs from a job queue.
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Urgent
 
Similar Threads
calling one servlet from another servlet in separate JVM's
RequestDispather
How to get JSP URL in Servlet
Servlet Lifecycle Doubt?
contextScope attribute setting