Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Multithreading

 
Saurabh Agarwal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saurabh Agarwal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic