Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple user servlets 1

Status
Not open for further replies.

rorymurray

Programmer
Oct 16, 2001
44
AU
I am developing a servlet that will be avilable to multiple users to create their own timetable.
I store users classes in an array, then display the timetable array.
My only problem is that when multiple users are using it at once, I need a new instance of the servlet to be created, so the users don't put their classes into the same arrays.

Any pointers?

Thanks
 
What web server are you using? That should be handled for you by the Servlet engine your running under.

-pete
 
Oracle BC4J webserver...

A friend told me that there is just one servlet instance unless you specifically invoke another one. So how do I do that?
 
Why don't you store each users timetable array in their Session object? Each session then has it's own array, which the servlet can pull from the Session object and update.
 
idarke,

Thanks for the rescue, I was totally lost [lol] now I see what he was getting at. [hammer]


-pete
 
NEVER use classlevel parameters in servlets to store data that is to be shared between requests as it's shared by ALL requests by ALL clients.
The session is indeed the best way to do it (or a database and pass a userid code in the session only, depending on the size of the data).
 
Thanks for the tips.

I am new to servlets, so how do i store the array in the session object? Is a new session object created every time a servlet is invoked by each user?
 
Only a single instance of a servlet gets created, with each user request resulting in a new thread that is handed off to doGet or doPost as appropriate.

When the servlet is first created, its init method is invoked, so that is where you put one-time setup code. After this, each user request results in a thread that calls the service method of the previously created instance.
Multiple concurrent requests normally result in multiple threads calling service simultaneously, although your servlet can implement a special interface (SingleThreadModel) that stipulates that only a single thread is permitted to run at any one time.
The service method then calls doGet, doPost, or another doXxx method, depending on the type of HTTP request it received. Finally, when the server decides to unload a servlet, it first calls the servlet’s destroy method.

Hopes this makes things clear to you.
Fabian Dierckxsens
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top