• Post Reply Bookmark Topic Watch Topic
  • New Topic

servlet instance

 
Sani babangida
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am kind of new to servlet. My question is if i have a static variable in a servlet and a user
accessing a web page changes the value of the variable, will it affect the value that another user
is viewing giving that diffent instances of servlet are instantiated for different folks
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be only one servlet instance per JVM (one servlet for one servlet declaration in DD).
 
Sani babangida
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Georgy Bolyuba:
There will be only one servlet instance per JVM (one servlet for one servlet declaration in DD).


So are you implying if a user changes the value of a static variable, the change would reflect across all users of the servlet web page
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sani babangida:


So are you implying if a user changes the value of a static variable, the change would reflect across all users of the servlet web page


Not only that, because there is only one instance of the servlet, even instance variables will be shared across all threads.

You must write servlets to be thread-safe.

That means avoiding both class and instance variables that are not meant to be shared across threads. And of course, read/write variables that are to be shared must be sychronized appropriately.

Best practices dictate avoiding read/write variables in servlets, either static or instance, in favor of scoped variables in the appropriate scope.
[ March 02, 2007: Message edited by: Bear Bibeault ]
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if the applications is clustered across multiple JVMs? Will the static variable be synchronized across JVMs as well?

-Cameron McKenzie
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clustering is, of course, its own can of worms. Are you saying that one can rely upon clustering to keep static servlet variables thread safe?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thread just made me think about the impact of clustering an app.

I've never put non-final, static variables in a Servlet, so I never really thought of what would happen in a clustered environment if you did. A genuine question.

-Cameron McKenzie
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I never really thought of what would happen in a clustered environment if you did


Well since we now have two instances (say) of a server running, we must have two instances of the servlets running, one in each JVM. So I guess each static variable would relate itself to the servlet it belongs to.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which would then put the two Servlets out of sync, which comes back to Bear's point.

So, can you do it? Yes. Will it work? Sometimes. Should you do it? Probably not.



-Cameron McKenzie
[ March 02, 2007: Message edited by: Cameron W. McKenzie ]
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which would then put the two Servlets out of sync


It would be the container's job to keep them in sync wouldn't it ?. In any case I would think twice before using public static variables or instance variables and when you couple that with clustering, the scenario can be quite rare.

I am also not certain about the granularity of the syncing. Say a servlet updates a public static variable and then crashes the very next line. Will the synced backup servlet contain the updated value ? I think it should not. Perhaps every complete execution of a doGet or doPost would be considered something like a transaction to commit changes to a synced servlet.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bottom line is that it is asking for trouble to use read/write class or instance variables in a servlet. And as the context scopes are availabe to store data in a safe fashion, there's no need, so don't do it.

The only variables I ever declare at class level in a servlet are static final variables.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Meyers:

It would be the container's job to keep them in sync wouldn't it ?.

Nope.

When I said about one instance of servlet I have talked about "one server" case (without clustering). If you have distributed web application you will have one instance of servlet per JVM. Fields will not be synchronized by containers.

Originally posted by Bear Bibeault:

The bottom line is that it is asking for trouble to use read/write class or instance variables in a servlet. And as the context scopes are availabe to store data in a safe fashion, there's no need, so don't do it.


Agree. Servlet should be stateless and thread-safe.

FYI: servlet context will not be synchronized by containers (in case of distributed web application). Only session will be synchronized.

Refs:
http://jcp.org/aboutJava/communityprocess/final/jsr154/index.html
SRV.2.2 Number of Instances
SRV.7.7.2 Distributed Environments
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!