Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet question...

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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?
thanks,
Alex
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Bill

------------------
author of:
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic