This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Is the values variables declared in Servlets changes when multiple requests comes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is the values variables declared in Servlets changes when multiple requests comes?" Watch "Is the values variables declared in Servlets changes when multiple requests comes?" New topic
Author

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

pramodh somashekara
Ranch Hand

Joined: Jan 30, 2006
Posts: 62
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

Joined: Apr 15, 2004
Posts: 25
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,


Eduardo dela Rosa<br />SCJP 1.4
pramodh somashekara
Ranch Hand

Joined: Jan 30, 2006
Posts: 62
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Aug 27, 2002
Posts: 455
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 ]

SCJP, SCWCD, SCBCD, Oracle Certified Professional (SQL n PL/SQL)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

"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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ganesh Chinni
Greenhorn

Joined: Dec 30, 2005
Posts: 24
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is the values variables declared in Servlets changes when multiple requests comes?
 
Similar Threads
Not able to get the last question
member variables in servlet
Interesting problem with Apache/Tomcat
What to do to make this web page thread safe?
JSP Includes