Httpsessionlistener Interface Monitoring website number of people online

Source: Internet
Author: User
Tags sessions

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)

    1. <?xml version= "1.0" encoding= "UTF-8"?>

    2. <Context>

    3. <manager classname= "Org.apache.catalina.session.PersistentManager" saveonrestart= "false" >

    4. <store classname= "Org.apache.catalina.session.FileStore"/>

    5. </Manager>

    6. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.