aspose file tools*
The moose likes JSP and the fly likes Access Counts in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Access Counts in JSP" Watch "Access Counts in JSP" New topic
Author

Access Counts in JSP

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, I have a simple website with the front page as a jsp
I would like to keep track of the number of visitors to the site.
Currently I'm using.



which works fine, except for the fact in the website, there is another link back to the front page, which causes the accessCount variable to increment by 1 again.

I just want this accessCount to increase for the first time a user goes to the front page.
I've tried using sessions, but I'm not really sure how it works
Any help would be great. Thanks
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Colin,
The ideas is to put something in the user's session when you first count them. If that attribute is still there, the page shouldn't count them again.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
OK, but I don't know how to do that?
Do you know how to code it???
[ August 29, 2005: Message edited by: colin shuker ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by colin shuker:
OK, but I don't know how to do that?
Do you know how to code it???

[ August 29, 2005: Message edited by: colin shuker ]


Colin,
Looking through your recent posts, I can see that several people (including myself) have given you detailed explanations for binding variables to scope - with code examples. I've also posted this link: http://csajsp-chapters.corewebprogramming.com/CSAJSP-Chapter13.pdf which is a chapter that covers the topic completely. Javaranch is not a code mill. The folks here take great pride in helping new developers learn the craft but will quickly grow to resent someone who appears to want the code written for him/her.

Why don't you try this out yourself (using some of the code examples already given to you) and/or read that chapter again to see if you can get a grasp on the topic? If you get stuck, post your code and someone will be happy to point out where you're going wrong.

Best of Luck.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, I finally managed to do it myself as you suggested. My solution is different to anything I've seen before since I'm using a Boolean object for the attribute of each session. I'm not totally sure if its right because I don't understand this subject, but it seems to be working fine.

Here is the code I inserted into my front page JSP (which is also linked to from other pages in the site)

Any thoughts about this code are welcome.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This looks good.
The object in session can be anything ("null" or "not null" is all you care about).

Why are you putting the "count" object into session?
Is it to make it available to other pages?

You may, at some point, want to consider saving this value to a file or database so that it doesn't get reset everytime you restart the server or re-load the app.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
The total number of vists (count) needs to be dislpayed in a separate file. I thought about using beans to do this, but I got really confused.
I think setting as an attribute of application works well, becuz its clear whats going on, you dont need to think about sessions.

I'm using this Boolean object, becuz before with the way I was doing it before, there was a page in the site that linked back to this front page, causing the count to go up by 1 again, which isnt what I want.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access Counts in JSP