public void doGet (HttpServletRequest req, HttpServletResponse res) throws
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. ); } } This HolisticCountertracks its own access count with the countinstance variable, the shared count with the classCountclass variable, and the number of instances with the instanceshashtable (another shared resource that must be a class variable). Sample output is shown in Figure 3-2. Figure 3-2. Output from HolisticCounter Servlet Reloading If you tried using these counters for yourself, you may have noticed that any time you recompiled one, its count automatically began again at 1. Trust us it’s not a bug, it’s a feature. Most servers automatically reload a servlet after its class file (under the default servlet directory, such as server_root/servlets) changes. It’s an on-the-fly upgrade procedure that greatly speeds up the development-test cycle and allows for long server uptimes. Servlet reloading may appear to be a simple feature, but it’s actually quite a trick and requires quite a hack. ClassLoaderobjects are designed to load a class just once. To get around this limitation and load servlets again and again, servers use custom class loaders that load servlets from the default servlets directory. This explains why the servlet classes are found in server_root/servlets, even though that directory doesn’t appear in the server’s classpath.
Hint: If you are looking for very good and affordable webspace to host and run your java hosting application check Sandzak.com java web hosting provider