Code
package com.chinaseacom.store.common;import javax.servlet.http.httpsessionevent;import javax.servlet.http.httpsessionlistener;public class mysessionlistener implements Httpsessionlistener { private long onlinecount; public void sessioncreated ( Httpsessionevent event) { this.onlineCount=this.onlineCount+1; //saved in Application scope event.getsession (). Getservletcontext (). SetAttribute ("Onlinecount", onlinecount); } &Nbsp; public void sessiondestroyed (httpsessionevent event) { this.onlinecount=this.onlinecount-1; Event.getsession (). Getservletcontext (). SetAttribute ("Onlinecount", onlinecount); } }
Configuration in Web. xml
<!--Configuring a custom listener-<listener> <listener-class>com.chinaseacom.store.common.mysessionlistener< ;/listener-class> </listener>
JSP page
${application.onlinecount}
To understand the session first you need to know a concept: The session is destroyed in only two cases:
First: The session invokes the Session.invalidate () method.
Second: The previous two requests exceeded the lifecycle time specified by the session. The life cycle time of the session can be configured in Web. Default 30 minutes
The following configuration can be done in Web. xml:
<session-config> <session-timeout>5</session-timeout></session-config>
Tomcat defaults to serializing all of its sessions when a application is stopped. The Tomcat installation directory \work\catalina\localhost\ the project name folder below you see a sessions.ser file , and then at the next startup, in deserialization, continue the session that has not expired.
Tomcat stops an app separately and needs to go to the Tomcat management interface
650) this.width=650; "title=" capture. PNG "src=" http://s3.51cto.com/wyfs02/M00/71/31/wKioL1XIngqzJBjzAAWHD9Ijw5Y729.jpg "alt=" Wkiol1xingqzjbjzaawhd9ijw5y729.jpg "/> But what if you don't want the session to serialize to a file?
Answer:
Create Yourwebapp/meta-inf/context.xml as follows: (Supplemental: Meta-inf directory should be the same as Web-inf directory)
<?xml version= "1.0" encoding= "UTF-8"?>
<Context>
<manager classname= "Org.apache.catalina.session.PersistentManager" saveonrestart= "false" >
<store classname= "Org.apache.catalina.session.FileStore"/>
</Manager>
</Context>
Reference article:
http://blog.csdn.net/yiyuhanmeng/article/details/8156247
This article is from the "Nothing-skywalker" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1683427
Httpsessionlistener Interface Monitoring website number of people online