aspose file tools*
The moose likes Servlets and the fly likes how to count number of visitors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to count number of visitors" Watch "how to count number of visitors" New topic
Author

how to count number of visitors

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
hello, i want to maintain how many number of visitors are hit to my webpage, and i don't want to use sessions for this, i want to do this only cookies,
can anyone tell me how to do this using servlet?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Can't be done with cookies. A cookie is stored on each individual client. How could it help you maintain the aggregate count of visits?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

What information are you going to store in those cookies? You can use the request object in your servlet to keep track of requests coming in on your site. You could also create a servlet filter to implement your own kind of user access logging: http://koenaerts.ca/using-servlet-filters-in-java-web-applications/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Koen Aerts wrote:You can use the request object in your servlet to keep track of requests coming in on your site.

No you can't. A new request is created for each visit, so any count stored in request context would be lost.

You could also create a servlet filter

A filter makes much more sense. The count could be maintained in a file, a database, or app context.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Bear Bibeault wrote:
Koen Aerts wrote:You can use the request object in your servlet to keep track of requests coming in on your site.

No you can't. A new request is created for each visit, so any count stored in request context would be lost.

I guess it depends on what information he is collecting to count "visitors". I've used request instances to keep track of IP addresses hitting the site, for instance. Those IPs were also used to provide more customized functionality based on the country the IP originated from etc. I guess first he needs to define what he means by "visitor". For instance if it's the same person who visits twice a day, once in the morning, once in the afternoon, every day for 1 week. Does that count as 7 visitors, or 14 visitors, or just one?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
well if i say i only want to keep records the number of visitors, than also cookie is required??
or any other way to do this??
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Please explain first what exactly you would store in the cookie. You don't really need cookies to just count visitors.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
actually i need to count number of visitors, and their name and emails, and off-course to store names and emails, i need cookies, but how do i count number of visitors first.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Ok, say I'm a user and I'm going to visit your site for the first time. I open my browser, point it to your site. What is going to happen? Will there be a cookie generated on my computer? What information will it have and where does it come from? Do I have to enter it somewhere before I gain access to your site? Once I log on to your site, what happens next? Let's say I come back to your site tomorrow. I already have a cookie on my computer. How is it going to be used when I go to your site again?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Would I count as two visitors if I connected at noon from my work computer and then at 8 PM from my home computer? Or only one?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
i think there will not be any cookie generated on your computer, if you just point my website, but once you log in to my website, the cookie will be generated that is of your username and password, and one more thing if i am not wrong, if you login to my website, the cookie will be generated only when to check the remember me on this site checkbox, but i m not sure about it.
isn't it?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2

Would I count as two visitors if I connected at noon from my work computer and then at 8 PM from my home computer? Or only one?


Each time whenever you hits the page, you will be counted as a new visitor.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Ok, that is some useful information. So once I go visit your site again tomorrow, and I already have a cookie, does that mean I don't need to log on again, or do I still need to?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
no you actually need to click login, but you don't need to enter your user name and password again, you just need to click login...
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Punit Jain wrote:

Would I count as two visitors if I connected at noon from my work computer and then at 8 PM from my home computer? Or only one?


Each time whenever you hits the page, you will be counted as a new visitor.

So on that page, you probably need to just store my account name and timestamp in a database somewhere. To count visitors, say for a certain day or time period, you just need to count the number of records in the table for the specified date range. The only purpose of the cookie, I guess, would be to log the user automatically on to your site, but in itself doesn't keep track of counting anything. It is your code on the server side that does that.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

So the business about logins and e-mail addresses is a red herring. (See Red herring if you aren't familiar with that expression.) The only rule for counting visitors is "1 request = 1 visitor". In which case a servlet filter which counts the number of requests would work just fine.

I would refer to those events as "visits" rather than as "visitors". I would find that much less confusing, if I have understood your requirement correctly.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
you probably need to just store my account name and timestamp in a database somewhere.


okay i got you, i only want to save the username and emails into my database and just need to count the username or emails to calculate the number of visitors, m i right??
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2

The only rule for counting visitors is "1 request = 1 visitor". In which case a servlet filter which counts the number of requests would work just fine.


okay i got it, i only need to count number of request to my site to count number of visits, but if i not use filters can i just simply count the requests to my site??
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Punit Jain wrote:
you probably need to just store my account name and timestamp in a database somewhere.


okay i got you, i only want to save the username and emails into my database and just need to count the username or emails to calculate the number of visitors, m i right??

That sounds correct. Basically a simple row count is all you need (i.e. "select count(1) as visit_count from user_visits;") I would also store the current timestamp for each record. It gives you more options later, for instance to count the number of visits per month over the last year, or the average number of visits per day for the last week, etc. Or it can even be used to delete visits older than, say 1 year, etc. etc.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Punit Jain wrote:

Would I count as two visitors if I connected at noon from my work computer and then at 8 PM from my home computer? Or only one?

Each time whenever you hits the page, you will be counted as a new visitor.

That would mean that if I were to go to this page, and hit the F5 key 10 times (reload page) then that counts for 11 visits...
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
Koen Aerts wrote:
Punit Jain wrote:

Would I count as two visitors if I connected at noon from my work computer and then at 8 PM from my home computer? Or only one?

Each time whenever you hits the page, you will be counted as a new visitor.

That would mean that if I were to go to this page, and hit the F5 key 10 times (reload page) then that counts for 11 visits...


No actually what i mean is that, if i simply count the number of visits, that time if i press f5 10 times, then that count for 11 visits but if if i m counting number of visitors(after login) that time it will count it only 1..
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Punit Jain wrote:
Koen Aerts wrote:
Punit Jain wrote:

Would I count as two visitors if I connected at noon from my work computer and then at 8 PM from my home computer? Or only one?

Each time whenever you hits the page, you will be counted as a new visitor.

That would mean that if I were to go to this page, and hit the F5 key 10 times (reload page) then that counts for 11 visits...


No actually what i mean is that, if i simply count the number of visits, that time if i press f5 10 times, then that count for 11 visits but if if i m counting number of visitors(after login) that time it will count it only 1..

I see, you want to count the number of times someone LOGS ON to the site, not just hits the page. You basically insert the record in the table each time a users authenticates to the site.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
what i want to do is,
suppose a user (say: A) logs in to my site, first time the page will show his user name and message "you have visit this site 1 time", and if A again login to my site the '1 time' should be '2 times', and if other user (say: B) login first time, it will say "you have visit 1 time" for user B.

Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Ah, some key information that we could have used in the original question I can see why you think it can also be done in cookies, however, if someone clears their cookies or simply blocks them, then it won't work. This is why you count on the server. You can still use the same mechanism mentioned above (insert row in DB, then count number of records for that user), you just only count the records where the username in the table matches the one used in the logon screen.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
got it, so the mechanism would be same, either i can do this using cookies or db (ie. i store the username and password in cookies.), but the only thing is if cookies are destroyed, no records/number of visitors will remain stored.
right???
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Punit Jain wrote:got it, so the mechanism would be same, either i can do this using cookies or db (ie. i store the username and password in cookies.), but the only thing is if cookies are destroyed, no records/number of visitors will remain stored.
right???

No, you don't count in the cookies. You only use the cookies to store the username, email, whatever. Nothing else. ONLY on the server side do you insert a record in the table, right when the user has successfully logged in, and right after that your code selects the number of records in the table where the username column value matches the username the visitor logged on with (example: select count(1) as login_count from user_visits where username = 'bill';). Because the DB and servlets are on the server, whatever happens to the cookies on the visitors home computers is irrelevant. You are using the cookies only to provide another way to log on to the site (so the user doesn't have to type anything again), but NOT to keep track of visits.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
okay, but suppose i m not using db, can i do like this:
i store username and password of the user in cookie and i get the username from cookie, and now i want to count number of visitor or number of time this user visits the page, is it possible??
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Punit Jain wrote:okay, but suppose i m not using db, can i do like this:
i store username and password of the user in cookie and i get the username from cookie, and now i want to count number of visitor or number of time this user visits the page, is it possible??

Yes, you could put a simple counter variable in the cookie, for instance "loginCount=23" and you increment the value each time after a successful login and store again in the cookie. But as mentioned before, if the user clears the cookies or simply blocks them, then this won't work. Personally each time I close my browser, all cookies are cleared. So if I were to use your site, I would always see "1 visit", even if I had been there 200 times. You have zero control over what a user does with his cookies, but you have all control over what happens on your server.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
yes i m not using that, but i just want to clear my concept, now i got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to count number of visitors