This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is the values variables declared in Servlets changes when multiple requests comes?

 
pramodh somashekara
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have declared few variables in a servlets. These variables in servlets get their values from the JSP page.

My question is, does the values of these variables get changed when multiple users requests gets executed simultaneously as the servlet/jsp is not declared as thread proof.

If these values get changed, how can I make variables retain their values till the session lasts?
 
Eduardo Dela Rosa
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It's not a good practice to store *value in non-final variable* in Servlet,
be it member or class variable.

If you're thinking of a *final* or *constant* value, that should be no
problem. BUT that's not your case.

Since you mention about session, better yet store your values as attributes
in the HttpSession object to maintain integrity, i.e., the value it holds
is local to the session that created it.

If you should really need a value to be shared by all Servlets, you still
have the option of using ServletConfig (per servlet) OR ServletContext (per
web app). They are read only once during web app initialisation, though.


Regards,
 
pramodh somashekara
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am not using any 'final' variables, what I meant was the values received from a "particular" user say user login.

I store this user login inside a variable in the servlet for future usage say to query something on the user login.

If another user logs in, does this new login overrites whatever was there before from the first user's login as the new user also executes same servlet simultaneously.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If another user logs in, does this new login overrites whatever was there before from the first user's login as the new user also executes same servlet simultaneously.

True. You appear to think that each user gets their own servlet and this is not the case, all requests use the same instance of a servlet.
 
Rajeev Ravindran
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from not storing values in final variables, do not declare the variables in class level; the variables declared as class level in a servlet will be shared by all users unless your servlet implements SingleThreadModel.

Its always safe to declare method level variables inside the service method of your servlet.

Thanks,
Rajeev
[ April 07, 2006: Message edited by: Rajeev Ravindran ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64713
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"pramodh bs",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
Ganesh Chinni
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To keep these values thread safe, store these values in request attribute or store them as local variables.

Do not store the userlogin value in a class variable.


Regards,
Ganesh
SCJP 1.4
SCWCD 1.4
SCBCD 1.3
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic