• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlets - static methods - User unique data - The Good, Bad & Ugly

 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks, I teach Java and have one of those classes right now that REALLY need to get down to the absolute whys, whens, & hows... I will say they are a great bunch because they are ALWAYS keeping me on my toes & challenging me....BUT I seem to have hit a major wall trying to explain to them why static in a servlet can be very dangerous. Im basically talking about a method that accepts USER UNIQUE data, does something with it for that specific user & then returns or outputs that data. I was taught many moons ago that static methods are just asking for trouble in this context as data can be polluted, especially with areas where thread safety is an issue & I always consider thread safety in my webapps!
What I was looking for was some of you folks input on the subject, esp. you big kids (Bill, Mike & Tony etc..)
They already know about thread safety & the dangers of class variables, static variables & how to handle real problems using the SingleThreadedModel so I am really looking to focus on the static method & the variables it accepts, manipulates & returns.
One question I actually thought of & suprised myself when I didnt know the answer is "Are variables accepted into a static method themselves automatically static?" At first glance it seems like a no brainer but when you look at it deeper (I may be looking too deep) I start to wonder what the answer really is.
Anyway, as always I welcome your input, thoughts & comments. Ive had my "kids" signed up here since day one of class because as I told them, "Most of the best learning I have had over the past 3 years has been right here at the ranch!" Thanks All!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets draw a clear distinction between static variables and static methods. Obviously static variables are visible to all requests just like instance variables and thus should never be used to store user/request specific data. There are some old threads here where people got bit by this.
Static methods on the other hand, I don't see as any more of a problem than instance methods. After all, there is only one servlet instance anyway so there is no reason to use a static method.
If you do, you would simply need to recognize situations in which you would have to synchronize the method and whether there would be conflicts with synchronized instance methods.
A synchronized static method and a synchronized instance method are locking different objects, so watch out!
Bill
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only problem I can think of is that there is no guarantee that the init() method has been called. So if your static method wants to use something set up by the init() method, it may or may not work correctly.
Other than that (AFAIK), there really is no general threading danger to doing so--it's just really bad form. (I even went and reviewed both the Java and Servlet specs before posting just to make sure.)
The reason it's bad form is because there is no good reason for an outside class to invoke a static method on a servlet. None.
Each thread has its own stack. The parameters are not themselves static. (Try changing one of your static methods to have a static parameter--say static String myArg, and see what happens anyway.) So as long as a static method does not modify/access class member variables, it is still thread safe without synchronizing it, which is obviously not a good idea anyway.
The one thing that making the method static in your servlet does that may be desired is make it implicitly final. But if that was your goal, why didn't you just declare it final anyway?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!