*
The moose likes JSP and the fly likes how we can count no.of hits on a JSP page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "how we can count no.of hits on a JSP page?" Watch "how we can count no.of hits on a JSP page?" New topic
Author

how we can count no.of hits on a JSP page?

ramu ta
Greenhorn

Joined: Feb 06, 2011
Posts: 18
how we can count no.of hits on a JSP page?
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

There are many ways, if you don't want to implement it your self, you can use some analytics service like, "google analytics" or statcounter. With this services you will get many more information like where the visitors came from, search engine keywords etc..

If you want to implement a hit counter your self.. see this JSP Life Cycle example. It explains exactly same thing, A request counter.


[Servlet tutorial] [Servlet 3.0 Cook Book]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

Wow! I hope that article is very old because it uses a lot of bad practices!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

The article is for absolute newbies, who doesn't have even basic idea's about JSP.. The article doesn't intend to explain how to write a good JSP page, the purpose is to let them understand the life cycle, and if ever they want, how they can declare variables, functions inside JSP. I know it's not a good practice to have jsp declare variables, use scriptlets and jspInit. But I believe, Still newcomers should know about those parts of JSP.
ramu ta
Greenhorn

Joined: Feb 06, 2011
Posts: 18
thank you
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

I disagree. Exposing newcomers to poor practices just instills them as a bad habit that needs to be broken later. Any article on JSP written after 2002 should pretend that scriptlets and scriptlet expressions do not even exist, or, at best, mention them in passing as something that needs to be avoided at all costs.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

ramu ta wrote:thank you

See? Now ramu is likely to go ahead and implement this using scriptlets and scriptlet declarations and that is a poor practice that he will have to unlearn later.

I'd use a filter to count hits and keep the goop off of the pages.
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

If you read at the end of the article, there's a note about the same thing.

To Ramu : As Bear said, its really not a good practice to implement it this way, It's just for absolute beginners to see log messages when a jsp is initialized, destroyed and when jsp receives a request, it's just an example and not some thing that you should copy to your production code. As Bear said, you can write a filter that will intercept all request to your jsp page and implement the counter there.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

Too little, too late, in my opinion. Let's hope people take heed of that.
ramu ta
Greenhorn

Joined: Feb 06, 2011
Posts: 18
ya, i knew the filter concept. i asked this question to know the otherway to count hits without using filter
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

ramu ta wrote:ya, i knew the filter concept. i asked this question to know the otherway to count hits without using filter

If you just want to try out, it's fine to implement the counter yourself, either filter or other way. But if you really want the page analytics, I would suggest get google analytics (If you must not have to).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

On that we can very much agree. The best place to count the hits is outside the page!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how we can count no.of hits on a JSP page?