• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Newbie Question

 
Bing Lu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
pilania
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bing Lu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anyway to do it w/o using sessions
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

If you wrote a servlet called "PageRequestor"

Every single link (EVERY LINK) that you would ever have on your site would always use the PageRequestor, so that your site URLS would look like: http://localhost:8080/webContext/PageRequestor?page=page1.jsp

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic