Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes JSP and the fly likes Website Hit Counter Jsp and Bean Based. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Website Hit Counter Jsp and Bean Based." Watch "Website Hit Counter Jsp and Bean Based." New topic

Website Hit Counter Jsp and Bean Based.

vinay abhishek

Joined: Nov 23, 2012
Posts: 18
I have developed a Jsp page which tracks every hit, It increments the count and stores it in the Database(MySql).
Now I would like to make the code Generic by putting the entire logic in the backing bean and provide a reference of the bean for every JSP page whose count I would like to monitor.
Here is the code.

Now How do I get started with the idea

Stefan Evans

Joined: Jul 06, 2005
Posts: 1518
What is it you are "counting" here?

The number of pages one user visits in your app?
You are inserting a new record at every session. I would check your database table to see how many rows it has right now. My guess would be it has several rows, all with the same value :-)

- Database queries/code are expensive. At the least use a pooled database connection rather than creating a new one each time.
- Shifting this logic into a java class is good
- I would suggest you take a look at using a Servlet Filter.
- Work out what you are counting (counts for each page? for each user? ) and store that information as well.

vinay abhishek

Joined: Nov 23, 2012
Posts: 18
In this way I was able to monitor the count based on a session and add it into the database.

I plan to use connection pool and java persistence API to have the back up for the count. That is fine.
But when I try to Generalize the code by building an enterprise java bean I am badly struck with it.
For example I try to monitor the hit rate of a JSF page. I annotate the give a Managed Bean annotation to the backing bean.
How do I call the counting logic that is in the bean class, in my JSF page? I assume that if I can pass an invisible or a secret value from the JSF to the count logic in the bean, it would count the number times page was hit. Is the a correct idea? If so how do I get Started? Help needed.
What if I want to check the hit rate for a plain HTML page? This is also a unsolved.
vinay abhishek

Joined: Nov 23, 2012
Posts: 18
Here when I say hit rate, I mean the number of times a page is accessed.
I agree. Here's the link:
subject: Website Hit Counter Jsp and Bean Based.
It's not a secret anymore!