Monitoring session creation and invalidation using the Httpsessionlistener interface

Source: Internet
Author: User

Transferred from: http://uule.iteye.com/blog/824115

Httpsessionlistener:

The session creation event occurs every time a new session is created, and a similar session failure event occurs every time a session fails.

This interface also contains only two methods, corresponding to the creation and invalidation of the session, respectively:
# public void sessioncreated (httpsessionevent se);
# public void sessiondestroyed (httpsessionevent se);

My web App wants to know exactly how many users are using it?

Statistics on the number of people online are often required on the website. In the past, the general practice is to combine login and exit functions, that is, when the user enters the user name password to log in when the Counter plus 1, and then when the user clicks the Exit button to exit the system when the counter minus 1. This processing method has some shortcomings, such as: After the user login, may forget to click the Exit button, and directly close the browser, resulting in a counter minus 1 of the operation is not implemented in a timely manner, there are often some content on the site is not required to access the website, in this case can not be used in the above method of online population statistics.
We can use the event Listener (Listener) defined in the servlet specification to solve this problem and achieve more accurate online demographics. For each user being accessed, the Java EE Application Server creates a corresponding HttpSession object for it. When a browser accesses a website for the first time, the Java EE Application Server creates a new HttpSession object and triggers the HttpSession creation event, if the Httpsessionlistener event listener is registered, The sessioncreated method of the Httpsessionlistener event listener is called. Conversely, when the browser accesses the end timeout, the Java application Server destroys the corresponding HttpSession object, triggering the HttpSession destroy event, The Sessiondestroyed method that invokes the registered Httpsessionlistener event listener at the same time.

Java code
  1. Import Javax.servlet.http.HttpSessionListener;
  2. Import javax.servlet.http.HttpSessionEvent;
  3. Public class Sessioncounter implements Httpsessionlistener {
  4. Private static int activesessions =0;
  5. /* Session Creation Event */
  6. Public void sessioncreated (httpsessionevent se) {
  7. ServletContext CTX = Event.getsession (). Getservletcontext ();
  8. Integer numsessions = (integer) ctx.getattribute ("numsessions");
  9. if (numsessions = = null) {
  10. Numsessions = New Integer (1);
  11. }
  12. else {
  13. int count = Numsessions.intvalue ();
  14. Numsessions = New Integer (count + 1);
  15. }
  16. Ctx.setattribute ("Numsessions", numsessions);
  17. }
  18. /* Session Failure Event */
  19. Public void sessiondestroyed (httpsessionevent se) {
  20. ServletContext ctx=se.getsession (). Getservletcontext ();
  21. Integer numsessions = (integer) ctx.getattribute ("numsessions");
  22. <span class="Oblog_text" > if (numsessions = = null)
  23. Numsessions = New Integer (0);
  24. }
  25. else {
  26. int count = Numsessions.intvalue ();
  27. Numsessions = New Integer (Count- 1);
  28. }
  29. Ctx.setattribute ("Numsessions", numsessions);</span>
  30. }
  31. }

in this solution, whenever a session is created or destroyed, Sessioncounter is notified of this class, and of course the reason for this is that the configuration must be done in the Web. xml file. such as the following configuration code:

Java code
    1. <listener>
    2. <listener-class>demo.listener.sessioncounter</listener-class>
    3. </listener>

Sessiondestoryed (Session destruction) events occur in the following two scenarios:
1. When the Session.invalidate () method is executed.
Since Session.invalidate () is executed in Logoutservlet.java, Sessiondestory () is triggered to clear the current user from the online user list. We don't have to manipulate the online list in Logoutservlet.java, so Logoutservlet.java's content is now.

Java code
    1. Public void Doget (httpservletrequest request,httpservletresponse response)
    2. throws Servletexception, IOException {
    3. //Destroy session
    4. Request.getsession (). invalidate ();
    5. //Success
    6. Response.sendredirect ("index.jsp");
    7. }

2.
If the user does not have access to the server for a long time, the server will automatically destroy the timeout session if the maximum timeout period is exceeded.
The session timeout can be set in Web. XML, in order to easily see the timeout effect, we set the timeout to the minimum value.

Java code
    1. <session-config>
    2. <session-timeout>1</session-timeout>
    3. </session-config>

The time unit is one minute and can only be an integer, and if it is 0 or negative, the session will never time out.

2.HttpSessionEvent

This is a class that represents a change in a Web application within a session event notification.

Java code
  1. Public class Shopsessionlistener implements Httpsessionlistener {
  2. public void sessioncreated (httpsessionevent se) {
  3. }
  4. public void sessiondestroyed (httpsessionevent se) {
  5. String SessionID = Se.getsession (). GetId ();
  6. Eopsite site = (eopsite) threadcontextholder.getsessioncontext (). getattribute ("Site_key");
  7. if (site!=null) {
  8. Icartmanager Cartmanager = Springcontextholder.getbean ("Cartmanager");
  9. Cartmanager.clean (Sessionid,site.getuserid (), Site.getid ());
  10. }
  11. }
  12. }

Se.getsession (). GetId ();

GetID () in the HttpSession interface:

Returns A string containing the unique identifier assigned to this session.

Returns a string that contains a unique identifier assigned to this session.

Monitoring session creation and invalidation using the Httpsessionlistener interface

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.