?
Solved

Get Other Server Sessions

Posted on 2005-04-22
11
Medium Priority
?
214 Views
Last Modified: 2010-05-18
Is there a way using Tomcat 5.0 to access the total number of sessions that tomcat is serving up for a given webapp from a webapp, like the manager application shows?

Example: I want to be able to show my visitors how many people are viewing the site at a time.
0
Comment
Question by:sniop1
  • 7
  • 4
11 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 13848190
Mmm....

I have not found that situation yet. But what you can do is that you can have one counter that will be incremented once the session is created. Then afterward, you will display this counter to your visitor. There are many alternatives to do it. First, you can simply manually it like

/* Sample 1 */
session.setAttribute("USER_SESS", login);
counter++;
/ * End of Sample 1 */

Or you may want your class to implements javax.servlet.http.HttpSessionListener or javax.servlet.http.HttpSessionBindingListener and put your increment in public void sessionDestroyed(HttpSessionEvent e)
 and public void valueUnbound(HttpSessionBindingEvent arg0) respectively.

Look at this.

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21398382.html

I hope that helps.

Regards
Dave


0
 

Author Comment

by:sniop1
ID: 13848247
2 things:

1. this is solely a JSP application, no servlets or classes are involved
2. If i am not mistaken, setting an attribute using session.setAttribute(name,val) will only set the attribute for the current session, how do i make there be server wide attributes that i can set across all sessions?

Thanks
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13848287
Hi,

"this is solely a JSP application, no servlets or classes are involved"

Then where do you create your session (and to be displayed afterward)?

For you second question, it does not matter, the thing that we are interested in counting the number of active sessions in the web app itself.

Regards
Dave
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sniop1
ID: 13848336
I thought that the session.setAttribute only worked for the current session, so other sessions could not see it.

aka if user a calls session.setAttribute("no_sessions",1+Integer.parseInt((String)session.getAtribute("no_sessions")));

and then a minute later user b calls the same thing, it would not compound the results

and the session object (referenced by a cookie that tomcat places on the user's machine) is created by tomcat upon compilation of the jsp page, in the _jspService method

the session object is creating using JspFactory.getDefaultFactory().getPageContext(this,request, response, null, true, 8192, true).getSession();
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13848367
Oh yes,

Sorry, I think that I did not understand your question. In my previous post, you will not set the counter into session.

So the concept is that, you will have many sessions but you will only have one counter that will be incremented if the new session is created and decremented if the session is destroyed. So there will only be one instance of counter.

Let the counter sit only on your server. If you want to display it into visitor then you will call this counter. By this point, it is okay for you to put the counter in session or in request.

Does it help you to understand better?

Regards
Dave
0
 

Author Comment

by:sniop1
ID: 13848386
How would you reccommend storing this counter. Evidently tomcat has one somewhere because it shows it in the manager application. I have thought about making the counter part of the SQL database that this application uses, but if the user closes the browser rather than using the logout jsp i wrote then the counter would not decrease, even though their session is no longer active. Any ideas?
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13848398
Hi,

For recommendation to store the counter, I have no idea other than to use Servlet (some mechanism of static variable) or etc. Let the counter to To save it in DB, I will put it on second priority since everytime the user creates or destroys session, you need to access DB.

It is true that if the user does not logout, the number of session will not be decreased. You can set your session into session.setMaxInactiveInterval(6000); which means that if the user does not access or do anything on session, the session will automatically be invalidated. Then you will need to decrement your counter in your method.

I hope that helps.

Regards
Dave
0
 
LVL 16

Accepted Solution

by:
suprapto45 earned 1500 total points
ID: 13848401
Hi,

Oh yes, 6000 means in second. So in my previous post, if the user does not access or do anything to session within 6000 seconds, the session will be invalidated.

Regards
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13848441
Hi,

Thanks. Glad I could help.

Regards
Dave
0
 

Author Comment

by:sniop1
ID: 13855329
I wanted to let you know that i did more research and found a better way:

I made a class SessionCounter that was a HttpSessionListener, then in my web.xml file i attached it as a <listener-class>

The SessionCounter class held on to an ArrayList of all the sessions, and when one becomes invalid, it is removed from the list. I then made acessor methods such as getActiveSessions (which returned the number of sessions in the list), and getAttributeFromAllSessions. This way i had access to all the sessions from one static class

regards
Charlie
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13864594
Hi Charlie,

Thanks so much.

Regards
Dave
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Integration Management Part 2
Screencast - Getting to Know the Pipeline
Suggested Courses

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question