Jsp code example for compiling current online count statistics

Source: Internet
Author: User

First, write a class:

  1. Import javax. servlet .*;
  2. Import javax. servlet. http .*;
  3. Public class SessionCounter implements HttpSessionListener {
  4. Private static int activeSessions = 0;
  5. Public void sessionCreated (HttpSessionEvent se ){
  6. ActiveSessions;
  7. }
  8. Public void sessionDestroyed (HttpSessionEvent se ){
  9. If (activeSessions> 0)
  10. ActiveSessions --;
  11. }
  12. Public static int getActiveSessions (){
  13. Return activeSessions;
  14. }
  15. }
  16. Configure web. xml
  17. <? Xml version = "1.0" encoding = "UTF-8"?>
  18. <Web-app version = "2.4"
  19. Xmlns = "http://java.sun.com/xml/ns/j2ee"
  20. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  21. Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee
  22. Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
  23. ****************************************
  24. <! -- Listeners -->
  25. <Listener>
  26. <Listener-class>
  27. SessionCount. SessionCounter (note here)
  28. </Listener-class>
  29. </Listener>
  30. **************************************** *
  31. </Web-app>
  32. Create a JSP test:
  33. Test. jsp
  34. <% @ Page language = "Java" contentType = "text/html; charset = GBK" %>
  35. <% @ Page import = "java. SQL. *" %>
  36. <% @ Page import = "SessionCount. SessionCounter" %>
  37. <Html>
  38. <Head>
  39. <Meta http-equiv = "Content-Type" content = "text/html; charset = GBK">
  40. <Title> untitled document </title>
  41. <Body bgcolor = "# FFFFFF">
  42. Online users: <% = SessionCounter. getActiveSessions () %>
  43. </Body>
  44. </Html>

Can it be implemented? I also found it. I used it and can make statistics.

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.