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 (perweb app). They are read only once during web app initialisation, though.

Regards,

pramodh somashekara

Ranch Hand

Posts: 62

posted 11 years ago

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.

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.