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



Joined: Jul 01, 2002
Posts: 1
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?
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
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.

"JavaRanch, where the deer and the Certified play" - David O'Meara
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
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.

"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The team, Newsletter 039.
I agree. Here's the link:
subject: SingleThreadModel
It's not a secret anymore!