• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Servlet Newbie Question

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there anyway to do it w/o using sessions
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic