aspose file tools*
The moose likes Servlets and the fly likes Servlet Multithreading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Multithreading" Watch "Servlet Multithreading" New topic
Author

Servlet Multithreading

Saurabh Agarwal
Greenhorn

Joined: Aug 27, 2007
Posts: 22
Hi

I have to implement below scenario :

Servlet 1
{
call servlet2.
redirect to another jsp page.
}

In above code segment, servlet2 must execute in background.
And control must immediately be redirected to jsp page without waiting for servlet 2 to complete.
Normally this can be implemented using threading.

But how will threading be implemented in servlets.

Thanks
Add to Saurabh321's Reputation
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

You can implement it in the same way as any other application. But remember that you won't be able to return any value from servlet2 to the client as there won't be any output stream available to write to the client...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
Hi Saurabh,

In servlet multithreading on request basis,
If two client call one servlet then container will start new two thread for two requests,
In your code,
Servlet 1
{
call servlet2.

redirect to another jsp page.
}

above quoted code is either forward or include...

in forward case, control is not returns it forworded to servlet2(here it will not redirect to jsp page)
when user call Servlet1 container starts thread1(suppose) when we forward to servlet2 container will call servlet2 and execute service method in thread2

In include case, it will call servlet2 and execute it all & write response of that servlet in Servlet1's response object that we passed to include method while including servlet2 and then redirect to jsp page.
when user call Servlet1 container starts thread1(suppose) when we include to servlet2 container will call servlet2 and execute service method in thread2 after that thread2 execution is completed with servlet2 service method and it returns to thread1 which is executing servlet1 service method.

Thanks,
Shailesh
Saurabh Agarwal
Greenhorn

Joined: Aug 27, 2007
Posts: 22
Thanks Sailesh.

But problem with include is..
w will have to wait for servlet 2 to complete, then only response will be redirected to jsp page.

I cannot wait for servlet 2 to complete its execution. as it takes 30 minutes.

Is there some other way
Saurabh Agarwal
Greenhorn

Joined: Aug 27, 2007
Posts: 22
Ankit Garg wrote:But remember that you won't be able to return any value from servlet2 to the client as there won't be any output stream available to write to the client...


I dont have to return anything to client.

but
Ankit Garg wrote:You can implement it in the same way as any other application

how to do it !

can you give some code idea for line 1 and line 2 of servlet1 below.
Servlet 1
{
Line 1: call servlet2.
Line 2: redirect to another jsp page.
}
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368

But problem with include is..
w will have to wait for servlet 2 to complete, then only response will be redirected to jsp page.

I cannot wait for servlet 2 to complete its execution. as it takes 30 minutes.

Is there some other way


that you can achieve by Thread class,
create one new class that extend Thread class and put all code in service of servlet 2 in the run() method of that new class,
and in service method of servlet2 create instance of thread class & call run() method on that instance.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Why do you want to run the background code as a servlet? Servlets are meant to service HTTP requests, not for background processing.

create one new class that extend Thread class

Don't do that; create a class that implements the Runnable interface instead.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Multithreading