How do I implement a hit counter in Servlets or JSP?

Alex Chaffee

When implementing a hit counter, there are many decisions to make.

  1. Are you sure you want to write it yourself?

    There are many free web-based hit counters out there that take care of a lot of the hassles for you. Most seem to serve up a GIF so you can embed it in your page as an IMG or JavaScript. See:

  2. What do you want to count?

    The most common thing to count is individual hits to your site's home page, via an IMG reference (see below). You may instead want to count unique new user sessions (see the API for HttpSession for information on setting up a listener that listens for new sessions), or some other factor.

  3. Where do you want to save the data?

    Servlets are transient. If you want your hit count to last past a server restart, you need to save it somewhere. This means using either a file system or a database. See

    Aron Tunzi posted some great code for writing to a file in the forum thread for this question, at How do I implement a hit counter in Servlets or JSP

  4. How do you want to display the results?

    You can either display the results as an image, or as embedded text or HTML. See below for information on image-based hit counters. To use embedded text, the easiest way is to write your hit counter as a JavaBean, with separate methods for incrementing the count and returning the current count. Then you can instantiate the bean, place it in the application context, and access it from any servlet using code like:

     HitCounter h = (HitCounter)getServletContext().getAttribute("hitcounter");
     h.incrementCount();
     out.print(h.getCount());
    

    Returning an image simplifies some aspects of the system but means you have to deal with creating an offscreen image and rendering the text inside it.

    You may also want to make your hit counter be an applet. See How can I add a counter applet to my web page? and How can I pass an object from an applet to a servlet? for help there.

How to build an image-based counter

To build an image-based counter, write a servlet -- let's say /myapp/counter -- that works as follows:

public void doGet(...) {
 incrementCount();
 Image i = getImage();
 writeImage(response);
}
Of course, you still have to write the incrementCount, getImage, and writeImage methods :-) See the AWT FAQ and the Servlets: Images topic for help on creating and sending images from a Servlet.

In your client pages, output the following HTML:

<IMG SRC="/myapp/counter">
and each client request will increment the count and display the image in a single shot.

(This simple architecture would have been possible for text too, if only Marc Andreesen had simply implemented client-side include in Netscape, instead of wasting all his time on tables and frames, which totally suck. Oh well, maybe next life.)

However, this technique is not always accurate, since it can count reloads, if the same user goes back to the home page. Also, you may end up counting hits to sub-pages too. Adding a parameter, as in IMG SRC="/myapp/counter?page=home" may help distinguish these case.

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact