File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes How to get the no.of website hits? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to get the no.of website hits?" Watch "How to get the no.of website hits?" New topic

How to get the no.of website hits?

Sv Kumar

Joined: Jan 04, 2002
Posts: 25
Hi All,
I want to get the no. of hits of our website using java .
For that ,whenever user opens a homepage,it can write a number in that
text file and when user next time visits, it adds a number to that file and display that number on the home page.But How to capture that url when user visits the home page for writing in the text file.
Can any body help..
Thanks in advance
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
You can get the URL by using the request object's getRequestURL method as follows:
String url = request.getRequestURL();
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

Hello, "Hello,"
... and welcome to the Ranch! It looks like you missed the sign on your way in about our policy on display names: your name has to be a real first and last name, or we get all ornery-like and lock you out. Please go here and update your display name to follow the rules. Thanks, pardner!
This is either a Servlets question, or maybe a web server question, but it really doesn't belong here. I'm going to send it to the Servlets forum for further discussion.
[ April 16, 2004: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Kyle Brown
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
If you have a web server in front of your site, it would be far, far easier simply to write a Java program (or go download a freeware one) that looks in your web server logs to provide you with this information.

Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at for other WebSphere information.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
I just recently wrote a class that does EXACTLY this. I looked and looked and couldn't find a java hitcounter I could easily add to my JSP pages so I wrote one. I'm going to post it on my site ( )
soon. Til then, here is some info to get you going. It was actually VERY simple to write.
Here is the beginning of the javadocs for it:
* To use this class, the only method you have to call is
* HitCounter.hit(String page) and give it a unique page String. This will
* return to you a String representing the current hit count (counting the
* current one). An example use in a .jsp might be:<BR>
* <%=HitCounter.hit("ErrorPage")%>
In the HitCounter class, it keeps a properties file of all the different hit counters. Anytime a hit() is recorded, it reads the current hit counter value from the in-memory properties object (or loads the file from disk if this is the first call to HitCounter), increments that page's counter, saves the .properties file, then returns the new count.
I started out not using a properties object and just making a new text file for each counter containing the current count, but I switched to the properties object because it seemed much cleaner.
The nice thing about this counter is that you don't have to do anything to create a new counter. Just add that one little line above in your JSP - no other configuration is necessary.
If you have other questions, let me know. I'll post the code soon.
[ April 21, 2004: Message edited by: Brian Pipa ]

My Java Freeware:<br />MACCC - <a href="" target="_blank" rel="nofollow"></a><br />Nator - <a href="" target="_blank" rel="nofollow"></a>
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63548

Aren't hit counters a bit, er, 1995-ish?

[Asking smart questions] [About Bear] [Books by Bear]
Brahim Bakayoko
Ranch Hand

Joined: Aug 29, 2003
Posts: 155
If your website uses jsps and/or servlets, simply create a filter with a class variable that you increment. Futher, save the value in a text file or database at a certain threshold and/or when the filter gets detroyed.
Reading and writing to disk at every request to keep track of hits is simply bad.
The filter will also load the stored value at initialization from wherever you stored it. Place the hits value in the ServletContext or whatever context you feel like or a hashtable (whatever your design requires).
Map the filter to all URLs (*).
If your site mainly uses static html, you can still display the hits count on those static html pages, but only for IE browsers.
- create an Iframe on static pages that will display the hits count
- create a jsp that only shows the hits value and a filter (as explained above)
- use the jsp in the Iframes
[ April 22, 2004: Message edited by: Brahim Bakayoko ]

SCJP, SCWCD, SCBCD, IBM CSD WebSphere v5, <br />A+, MCP 2000 and 2000 server, CST, and few incompleted certification tracks.<br /> <br />Ivory Coast<br /> <br />Analyze your web Request/Response @ <a href="" target="_blank" rel="nofollow"></a> down for a while...
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
You said:
"Reading and writing to disk at every request to keep track of hits is simply bad."
Why do you say this? I'm curious. With every web page that gets retrieved from ANY webserver, numerous log files are written to (server.log, access.log, etc.) that logs many different things about the client like IP address, browser used, etc. Why would writing to a file to keep track of hits on a particular page be any different?
I actually changed my code and it now stores all the hit counters in one .properties file. I can easily change it so that it only writes to the file after every X hits if I wanted to.
I'm just curious why you say writing to a file for every hit is bad. Maybe you know something I don't know.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
You said "Aren't hit counters a bit, er, 1995-ish?"
Yes, they are actually In my case, I'm rolling out a small web-app that is part of a much bigger corporate web-app and we really have no idea how often my piece will be used. There are many options for the web-app we can set depending on the kind of load it gets to optimize performance but we don't know what the optimum is since we don't have any usage patterns. The easiest thing I could think of to measure how often it gets used is to create a hitcounter on the page. The user can't actually see the hitcounter since it's in white text on a white background at the bottom of the page, but it can be seen by highlighting the area.
I know I could view/parse the WebLogic server logs, but the logs are huge (30+MB) and I don't have full access to the production server to poke around in the logs so this seemed like a decent way to do it. Once we get a handle on the usage, I'll probably remove the hitcounter.
If anyone has a better suggestion on a quick, easy way to accomplish this, please let me know.
I agree. Here's the link:
subject: How to get the no.of website hits?
It's not a secret anymore!