What (if any) is the difference between storing a variable in the ServletContext and just having it as a public static member of one of the classes?
Instead of writing:
// simplified (!)
int counter = (Integer)getServletContext().getAttribute("counter");
counter++;
this.getServletContext().setAttribute("counter", counter);
Why not just have:
// in class MyServlet
public static int counter = 0;
// in a method somewhere
MyServlet.counter++;
(Ignore concurrency issues, please, this is just a dumb example)
From what I can tell, these two options behave the same way under Tomcat. Is there something better about using the first option?