title: Servlet context scope vs global variable
body: <p>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?</p>
  
  <p>Instead of writing:</p>
  
  <pre><code>// simplified (!)
  int counter = (Integer)getServletContext().getAttribute("counter");
  counter++;
  this.getServletContext().setAttribute("counter", counter);
  </code></pre>
  
  <p>Why not just have:</p>
  
  <pre><code>// in class MyServlet
  public static int counter = 0;
  
  // in a method somewhere
  MyServlet.counter++;
  </code></pre>
  
  <p>(Ignore concurrency issues, please, this is just a dumb example)</p>
  
  <p>From what I can tell, these two options behave the same way under Tomcat. Is there something better about using the first option?</p>