• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

servlet question...

 
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
 
Author and all-around good cowpoke
Posts: 13078
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?
 
author
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).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic