File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Threads in JSP's and servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Threads in JSP Watch "Threads in JSP New topic

Threads in JSP's and servlets

Sreenivas Makala

Joined: Jun 14, 2001
Posts: 4
Is it a wise decision to have multi threaded code in Servlets and JSP's as they themselves are internally multi threaded?
Can anyone give me specific situations where you can go for this design decision?
Thanks in Advance
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
You should consider using threads when you have clients accessing shared resources:
HTTP servlets are typically capable of serving multiple clients concurrently. If the methods in your servlet do work for clients by accessing a shared resource, then you must either:

Synchronize access to that resource, or

Create a servlet that handles only one client request at a time

you may read some more at:
sandeep Gajbhiye

Joined: Jul 13, 2001
Posts: 3
Hi Sreenivas
U should avoid using threads in servlets and JSP.
But in one scenario threads are useful.
Suppose u have an Request which takes lot of time to process(for e.g an execution of procedure)
then u can execute that procedure in a thread and return reponse to the request .
This will prevent Browser Timeout to occur.
After u finish executing procedure u can set an attribute in session and return the reponse u wanted to send.
hope this is useful to u.
John Aiken

Joined: Jul 17, 2001
Posts: 3
As the previous poster has said, sometimes it can be very useful to have threads in a web application perform background tasks while the client session can proceed in parallel. The negative sides to this is obviously using server resources as having a large number of threads executing may slow the server significantly enough to affect new incoming requests. Using resources this way may make sense if you are servicing a paying customer but may not make sense if its guest users.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Your "thejavagod" name does not comply with the JavaRanch naming policy. Please spare a moment and re-register with a name that meets the requirements.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Sreenivas Makala

Joined: Jun 14, 2001
Posts: 4
I thank you all for your valuable inputs
I agree. Here's the link:
subject: Threads in JSP's and servlets
It's not a secret anymore!