How do I implement a hit counter in Servlets or JSP?
5 posts in topic
Flat View  Flat View

Posted By:   Hema_Latha
Posted On:   Saturday, June 30, 2001 05:57 AM

I just want to get the number of hitcounts to a particual site.This should be displayed in the first page of our site.Obvoiusly the first page is the html page.How can I write?by using servlet or is there is any utility is there in the html itself.Is there is a need to create a table for storing the hit value? answer ASAP.
Thank u..

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

Posted By:   Alex_Chaffee  
Posted On:   Tuesday, July 17, 2001 10:16 AM

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

  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");

    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?
    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(...) {
Image i = getImage();

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:

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, 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

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

Posted By:   Anonymous  
Posted On:   Monday, July 9, 2001 07:32 AM

www.theCounter.com provides you to install the free counter on your site

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

Posted By:   Alex_Chaffee  
Posted On:   Monday, July 2, 2001 07:23 PM

Re: Web

Posted By:   Aron_Tunzi  
Posted On:   Monday, July 2, 2001 03:17 AM

You can code you application with .jsp or a HttpServlet. There isn't no big differences. A jsp is a servlet but you can isolate the logic of the business and the design.

If you want save the counter you can save the value in a text file. I think that a table is not the best solution: you need to create a connection to the database and a SQL statement and that need much time.

Bye Aron.

Re: Web

Posted By:   Kesavan_Srinivasan  
Posted On:   Sunday, July 1, 2001 11:13 PM


When u call the index page, try triggering a Servlet & fill the page hit counter. U can hv a better solution with JSP.

If u want any further msg write me at


About | Sitemap | Contact