• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet Multithreading

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic