JSP + servlet statistics of current online users

Source: Internet
Author: User

Implementation class: onlineuser. Java

Package COM. alpha. onlineuser; </P> <p>/* <br/> * info: count the number of online users. <br/> * Date: 17:45:55 <br/> * Auth: javaalpha <br/> */</P> <p> Import javax. servlet. servletcontext; <br/> Import javax. servlet. HTTP. httpsession; <br/> Import javax. servlet. HTTP. httpsessionevent; <br/> Import javax. servlet. HTTP. httpsessionlistener; </P> <p> public class onlineuser implements httpsessionlistener <br/>{< br/> Public void sessioncreated (httpsessionevent event) <br/> {<br/> servletcontext SCT = event. getsession (). getservletcontext (); </P> <p> integer onlineuser = (integer) SCT. getattribute ("onlineuser"); <br/> If (null = onlineuser) {<br/> onlineuser = new INTEGER (1 ); <br/>} else {<br/> int COUNT = onlineuser. intvalue (); <br/> onlineuser = new INTEGER (count + 1); <br/>}< br/> SCT. setattribute ("onlineuser", onlineuser); <br/>}</P> <p> Public void sessiondestroyed (httpsessionevent event) <br/> {<br/> servletcontext SCT = event. getsession (). getservletcontext (); <br/> integer onlineuser = (integer) SCT. getattribute ("onlineuser"); <br/> If (null = onlineuser) {<br/> onlineuser = new INTEGER (0 ); <br/>} else {<br/> int COUNT = onlineuser. intvalue (); <br/> onlineuser = new INTEGER (count-1); <br/>}< br/> SCT. setattribute ("onlineuser", onlineuser); </P> <p> // destroy session <br/> // httpsession session = event. getsession (); <br/> // session. invalidate (); <br/>}< br/>}

 

JSP page: Test. jsp

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %> <br/> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <body> <br/> current number of online users: <% = application. getattribute ("onlineuser") %> <br/> </body> <br/> </ptml> <br/>

Web. xml Listener Configuration:

<! -- Count the number of online users --> <br/> <listener-class> COM. webapp. onlineuser. onlineuser </listener-class> <br/> </listener>

Related Article

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.