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 ]
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
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 ( http://filenabber.com/free-java-classes ) 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. brian [ April 21, 2004: Message edited by: Brian Pipa ]
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="http://webtools.servehttp.com" target="_blank" rel="nofollow">http://webtools.servehttp.com</a> down for a while...
posted 13 years ago
Brahim, 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
Bear, 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. brian