Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Count of hits to a site.  RSS feed

 
abheeshek reddy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

i need the code snippet for " When ever a guest or user visits the site the count should get increase and it should display the count of that particular Guest or User like your 150 visitor".
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create and Save a static count variable in your database.
Retrieve the value whenever a new request comes in for the app, increment the value and show it to the visitor and update the value again..
This will be a very simple code and can be done in many ways including plain old java too...
so you can do the coding part I think so..
For simple example reefer to K&B HeadFirst Servlets & JSP..

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi abheeshek ,

Google "HttpSessionListener" . you will get information related to your requirment
[ December 06, 2008: Message edited by: seetharaman venkatasamy ]
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sudipto Shekhar:
Retrieve the value whenever a new request comes in for the app, increment the value and show it to the visitor and update the value again..

Are you suggesting a way to count the number of requests and not the number of distinct sessions(or users logged in)? And the approach you are suggesting to use a field in database to count the number of requests has to be done very carefully to ensure thread safety.
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by abheeshek reddy:


" When ever a guest or user visits the site the count should get increase and it should display the count of that particular Guest or User like your 150 visitor".


I think the word visit has nothing to do with sessions..What do you say?
The user can just visit the site and then without going any further quits.... The is no session in this i think? I have very little experience.... so please tell me if i am wrong somewhere

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sudipto Shekhar:
I think the word visit has nothing to do with sessions..What do you say?
The user can just visit the site and then without going any further quits.... The is no session in this i think?


Not really. session is nothing but representation of a user. a valid user should login . otherwise he is not a user for the particular website . correct me ,if i am wrong
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what about guests.. do their visit too creates a session?That may depend on what the guest does,like he may just check the number of visits to the site through a link provided on the site and come back to the main page,that too may create a session but what if he does nothing?Goes to the site, goes through the home page and quits... Will that too create a new session? And still you have to count the number of hits on the site.....




[ December 08, 2008: Message edited by: Sudipto Shekhar ]
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on your requirement , if you want to count the number of visitors to your site , where a visitor will browse through multiple pages of your site but still be counted as one visit then you have to maintain sessions. Getting the number of distinct users hitting the server is not possible unless we maintain sessions.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does "visits the site" mean? If the site consists solely of JSPs and servlets, then the counting could be done with a servlet filter. If there are also static HTML files then it gets more complicated.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For maintaining a count of the visitors to a site, I think there should be a fixed entry point to that site. A filter can be placed at this entry point to keep the count of visitors.
If the site has multiple entry points, keeping count may be tough and may need putting filters at all possible entry points(or a generic filter which filters all requests). And a visitor moving from one page to another on the same site should not be counted twice by the filter and that should be taken care of. This maybe done by only doing forward/include when moving from one page to another within the site. The filter by default would not be applied on include/forward.
 
r wa
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I totally agree with Satya Maheshwari
Filters are the best options to get the count of requests that invoke particular servlet or your particular page(welcome page)
just put an counter & increment it through servletcontext
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The initial question was how to count the visitors, not how to count the pageviews.

Implement HttpSessionListener and simply do a visitor++ inside the sessionCreated() method.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"r wa", please check your private messages for an important administrative matter.
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amol Nayak wrote:
Originally posted by Sudipto Shekhar:
Retrieve the value whenever a new request comes in for the app, increment the value and show it to the visitor and update the value again..

Are you suggesting a way to count the number of requests and not the number of distinct sessions(or users logged in)? And the approach you are suggesting to use a field in database to count the number of requests has to be done very carefully to ensure thread safety.



As far, as i know, SGBD is thread safe Why should we worry about that? MyISAM blocks the table while a thread(process) is doing it's business (increments the count of visitors) and InnoDB, moreover, blocks updated row.

Actually, if you are interested in visits, you must take in consieration requests and not sessions. Also, you may need to use some cooky, or IP, or both of them check, just for the sake of not incrementing visits for each page request.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!