Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads in JSP's and servlets

 
Sreenivas Makala
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 977
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://www.java.sun.com/docs/books/tutorial/servlets/client-interaction/index.html
 
sandeep Gajbhiye
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Bye
 
John Aiken
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks!
 
Sreenivas Makala
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thank you all for your valuable inputs
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic