Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets/ThreadSafe

 
Arul Raj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Weather servlets are ThreadSafe? Please Explain
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally servlets are not Thread safe because the same servlet object can be executing many request Threads. You can not rely on instance variables keeping information for a particular request. When designing a servlet this must be kept in mind at all times.
It is not that hard to design your servlet so that only variables that are unique to a given request / Thread are used. The API provides help with the HttpSession object that you can use to store variables for a particular user.
If you absolutely can't figure out a way to do your servlet without instance variables, then you can use the SingleThreadModel interface which forces the servlet engine to only allow one Thread to execute in a servlet object.
Bill

------------------
author of:
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic