wood burning stoves 2.0*
The moose likes Servlets and the fly likes servlet question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet question..." Watch "servlet question..." New topic

servlet question...

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
If I have a webiste that uses a servlet, and this servlet records username and password of a user that logs into my site. Does each client that access my servlet gets his own instance of the servlet? If not, should I use threads to ensure that each client will get his own share of servlet's resources?

All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12809
The Servlet APIs are designed to take care of this problem.
Normally only one instance is created, however, each request is handled in its own Thread, with its own request and response objects. Assigning threads is handled by the servlet engine, you don't have to worry about it.
You only have to worry about synchronization when more than one request may access an object at the same time. Any good servlet book will explain this in detail.

author of:
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
but how do I synchronize w/o actually creating threads? In order to make synchronize a method don't I have to create thread first?
Kyle Brown
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
No, you don't have to make a new thread. As the previous poster explained, each HTTP Request that comes into the servlet is already on its own thread. Basically, the servlet framework is handling spawning all of the threads for you.
I second the advice to go read a good servlet book. In particular, pay attention to the part on threading, and also the part about HttpSession (which is the way to avoid having to do any synchronization).
Kyle Brown
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
[This message has been edited by Kyle Brown (edited October 04, 2001).]

Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
wood burning stoves
subject: servlet question...