0

I have the following piece of code its working fine,no issues but i am confused,how to make more than one instances of this.Every time i run this i get "there are 1 instances running"

public class HolisticCounter extends HttpServlet {


    private static final long serialVersionUID = 1L;
static int classCount = 0;  // shared by all instances
  int count = 0;              // separate for each servlet
  static Hashtable instances = new Hashtable();  // also shared

  public void doGet(HttpServletRequest req, HttpServletResponse res) 
                               throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();

    count++;
    out.println("Since loading, this servlet instance has been accessed " +
                count + " times.");

    // Keep track of the instance count by putting a reference to this
    // instance in a Hashtable. Duplicate entries are ignored. 
    // The size() method returns the number of unique instances stored.
    instances.put(this, this);
    out.println("There are currently " + 
                instances.size() + " instances.");

    classCount++;
    out.println("Across all instances, this servlet class has been " +
                "accessed " + classCount + " times.");
  }
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
Rookie
  • 8,660
  • 17
  • 58
  • 91

1 Answers1

4

In the vast majority of cases, there is only ever one servlet instance per <servlet> entry. The container does not need to instantiate any more, and so it reuses the same instance for every request.

The server is permitted to instantiate several, but there's usually no reason for it to do so.

skaffman
  • 398,947
  • 96
  • 818
  • 769