• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access Counts in JSP

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34218
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic