I have a Servlet that implements SingleThreadModel interface, and contain a instance variable (for example int count). The doGet(...) method increment and display this variable.The servlet name in the web.xml is: servlet1. For a SingleThreadModel servlet there is a pool of instances for each call with servlet1. Is the value of count variable consistent between servlet1 calls? Why?
Joined: Sep 29, 2000
Veri, Please change your name to be compliant with JavaRanch's naming policy. Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name. You can change your name: here. Thanks, Cindy
"JavaRanch, where the deer and the Certified play" - David O'Meara
Joined: Dec 19, 2002
Not necessarily. If the servlet engine spawns multiple instances of your servlet, each one will initialize its own instance variable, starting at zero. To make it consistent, you need to make your variable static and use a synchronized get method. If you only ever have one user, you might survive. :roll:
"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.