File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlet Newbie Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Newbie Question" Watch "Servlet Newbie Question" New topic

Servlet Newbie Question

Bing Lu
Ranch Hand

Joined: Oct 10, 2000
Posts: 42
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

Joined: Mar 20, 2000
Posts: 26
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

Joined: Oct 10, 2000
Posts: 42
Is there anyway to do it w/o using sessions
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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 agree. Here's the link:
subject: Servlet Newbie Question
It's not a secret anymore!