Count the number of online users in J2EE -- httpsessionbindinglistener and httpsessionlistener

Source: Internet
Author: User

Javax. servlet. http. httpsession is very common. When you browse a website, a session is generated no matter whether you log on or not. It records the data related to the viewer.

 

Two listener interfaces of one session

 

1 javax. servlet. http. httpsessionbindinglistener;

Classes that implement this interface will start with corresponding methods in the interface whenever they are stored in or removed from the session to achieve the listening effect.

 

2 javax. servlet. http. httpsessionlistener;

The class that implements this interface is itself a listener. Every time a session is generated or eliminated, the corresponding method in the interface is triggered to achieve the effect of the listener;

The implementation class of httpsessionlistener is itself a listener. to use it, you need to start this listener in Web. xml. For example:

<Listener>
<Listener-class> packname. classname </listener-class>
</Listener>

3. The two listeners are different.

The trigger of the former depends on the attributes added to the session.

The trigger of the latter depends on the generation and extinction of the session itself.


4. Listening to servletcontext and request are similar to the above.

 

 

Application 2-List of current online users

 

The number of online users list works for all users. Data should be stored in the application, that is, the servletcontext object.

 

Onlinedemo. Java

Package demo;

Import java. util. arraylist;
Import java. util. List;
Import javax. servlet. servletcontext;
Import javax. servlet. servletcontextevent;
Import javax. servlet. servletcontextlistener;
Import javax. servlet. http. httpsessionattributelistener;
Import javax. servlet. http. httpsessionbindingevent;
Import javax. servlet. http. httpsessionevent;
Import javax. servlet. http. httpsessionlistener;

Public class onlinedemo implements servletcontextlistener, httpsessionlistener, httpsessionattributelistener {

// Declare a servletcontext object
Private servletcontext application = NULL;

Public void contextinitialized (servletcontextevent SCE ){
// When the container is initialized, an empty container is stored in the application.
This. Application = SCE. getservletcontext ();
This. application. setattribute ("alluser", new arraylist ());
}

Public void contextdestroyed (servletcontextevent SCE ){
}

Public void sessioncreated (httpsessionevent SE ){
}

Public void sessiondestroyed (httpsessionevent SE ){
// Delete the user name from the list
List L = (list) This. application. getattribute ("alluser ");
String value = (string) se. getsession (). getattribute ("uname ");
L. Remove (value );
This. application. setattribute ("alluser", L );
}

Public void attributeadded (httpsessionbindingevent SE ){
// If the login succeeds, the user name will be saved in the list
List L = (list) This. application. getattribute ("alluser ");
L. Add (SE. getvalue ());
This. application. setattribute ("alluser", L );
}

Public void attributeremoved (httpsessionbindingevent SE ){
}

Public void attributereplaced (httpsessionbindingevent SE ){
}
}

 

Online. jsp

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. util. *" %>

<Form action = "online. jsp" method = "Post">
<Input type = "text" name = "name">
<Input type = "Submit" value = "login">
<A href = "logout. jsp"> log out </a>
</Form>
 
<%
If (request. getparameter ("name ")! = NULL)
Session. setattribute ("uname", request. getparameter ("name "));
%>
<H2> online staff </H2>
<HR>
<%
List L = (list) application. getattribute ("alluser ");
For (Object O: l)
Out. println (O );
%>

 

 

Logout. jsp

<%
Session. invalidate ();
%>

 

Original post address: http://blog.sina.com.cn/s/blog_705c12080100nayb.html

 

 

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.