This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
Hi, can anyone tell me how can I keep track of the number of users that are currently viewing a particular page using servlet, and is there any feedback once the user leaves the webpage produced by a servlet? thank you very much
one way is if you have loggin mechanism or when user hits your server keep a hashtable/dbtable of each user session. So depending on number of entires in your hashtable/dbtable at any time you can find out how many users are surfing the net.
Here is something completely off the top of my head. I thought this would work at first, but now I realize it won't at all.
My first thought was: Use < %! tags. This is a class wide variable. So that means it is outside the service method of the compiled JSP. So... You would have something like: But this only counts page 'views', not a 'current number of people viewing the page'. Second idea:
Inside the servlet, there would be (an array? / a Vector?) of all the pages on your site. You would then bump up the count on that page by one.
But whoops... how do you subtract pages? You would have to look at the incoming request, and perhaps you could use a 'referrer' (parameter/field?). It's used by websites to tell them from where their visitors came. (what was the last page you were just on?). So maybe you could use that to subtract... If someone was 'just on' page1.jsp, then you'd subtract one from that page.
The only problem is some security-conscious folks have software that blocks this information from your servlet. So it couldn't be used reliably.
So the final story is: I'm really not sure how to do it without sessions. The web is stateless. If you want to add and subtract a running count of "how many people are here right now?", this is a good example of maintaining state... which the web just doesn't do.