[Servlet] configuring listener listeners (httpsessionlistener and httpsessionbindinglistener)

Source: Internet
Author: User

1. Learn how to use httpsessionlistener to listen to session destruction.
2. Learn how to use httpsessionbindinglistener to listen to session destruction.

1. Compile an onlineuserlistener using httpsessionlistener.

Package Anni; </P> <p> Import Java. util. list; <br/> Import javax. servlet. servletcontext; <br/> Import javax. servlet. HTTP. httpsession; <br/> Import javax. servlet. HTTP. httpsessionlistener; <br/> Import javax. servlet. HTTP. httpsessionevent; </P> <p> public class onlineuserlistener implements httpsessionlistener {</P> <p> Public void sessioncreated (httpsessionevent event) {<br/>}</P> <p> Public void sessiondestro Yed (httpsessionevent event) {<br/> httpsession session = event. getsession (); <br/> servletcontext application = session. getservletcontext (); </P> <p> // obtain the logon username <br/> string username = (string) session. getattribute ("username"); </P> <p> // delete a user name from the online list <br/> List onlineuserlist = (list) application. getattribute ("onlineuserlist"); <br/> onlineuserlist. remove (username); </P> <p> system. out. println (userna Me + "timed out and exited. "); <Br/>}</P> <p>}

Onlineuserlistener implements two methods defined by httpsessionlistener: sessioncreated () and sessiondestroyed (). These two methods can be used to monitor the creation and destruction of sessions in the current application. Here we only use sessiondestroyed () to perform operations when the session is destroyed.

Get the session to be destroyed from httpsessionevent, get the username in the session, and delete it from the online list. The last line prints a message to the console, prompting that the operation is successful. This is only for debugging purposes and can be deleted during normal operation.

To make the listener function, we add it to Web. xml:

<Listener> <br/> <listener-class> anni. onlineuserlistener </listener-class> <br/> </listener>

In the following two cases, the sessiondestoryed (Session destruction) event occurs:
1. When the session. invalidate () method is executed.
Since logoutservlet. execute session in Java. during invalidate (), sessiondestory () is triggered to clear the current user from the online user list. the online list is operated in Java, so logoutservlet. the Java content is now like this.

Public void doget (httpservletrequest request, httpservletresponse response) <br/> throws servletexception, ioexception {<br/> // destroy session <br/> request. getsession (). invalidate (); <br/> // success <br/> response. sendredirect ("index. JSP "); <br/>}

2. If the user does not access the server for a long time and exceeds the maximum Session Timeout time, the server will automatically destroy the timeout session.
You can set the Session Timeout value in Web. xml. To make it easy to see the timeout effect, we set the timeout value to the minimum value.

<Session-config> <br/> <session-Timeout> 1 </session-Timeout> <br/> </session-config>The unit of time is one minute and can only be an integer. If it is zero or negative, the session will never time out.

The corresponding example is in 08-01. to verify whether the onlineuserlistener can be executed normally, we can log on to two users, one of which is logged out, and the other is waiting for one minute. Then we can see the output information in the console.
Download (13.11 KB)

2. Use httpsessionbindinglistener
Although httpsessionbindinglistener is called a listener, its usage is completely different from that of httpsessionlistener. Let's take a look at how it is used.

Our onlineuserbindinglistener implements the httpsessionbindinglistener interface. The interface defines two methods: valuebound () and valueunbound (), which correspond to Data Binding and unbinding events respectively.

To bind session data, call session. setattribute () to save httpsessionbindinglistener to the session. We will perform this step in loginservlet. java.

Public void valuebound (httpsessionbindingevent event) {<br/> httpsession session = event. getsession (); <br/> servletcontext application = session. getservletcontext (); </P> <p> // put the user name in the online list <br/> List onlineuserlist = (list) application. getattribute ("onlineuserlist"); <br/> // before use for the first time, you need to initialize <br/> If (onlineuserlist = NULL) {<br/> onlineuserlist = new arraylist (); <br/> application. setattribute ("onlineuserlist", onlineuserlist); <br/>}< br/> onlineuserlist. add (this. username); <br/>} 

This is the biggest difference between httpsessionbindinglistener and httpsessionlistener: httpsessionlistener only needs to be set to Web. XML to listen to all sessions in the entire application. Httpsessionbindinglistener must be instantiated and put into a session for listening.

Compared to the listening range, httpsessionlistener can listen to all sessions once. httpsessionbindinglistener is usually one-to-one.

This difference makes httpsessionbindinglistener advantageous. We can make each listener correspond to a username, so that we do not need to read username in the session every time, the Code of all online list operations can be moved to listener, which is easier to maintain.

The code for the valuebound () method is as follows:

Public void valueunbound (httpsessionbindingevent event) {<br/> httpsession session = event. getsession (); <br/> servletcontext application = session. getservletcontext (); </P> <p> // Delete the username from the online list <br/> List onlineuserlist = (list) application. getattribute ("onlineuserlist"); <br/> onlineuserlist. remove (this. username); </P> <p> system. out. println (this. username + "quit. "); <Br/>}

 

Here, you can directly use the username of listener to operate the online list without worrying about the existence of username in the session.

The trigger conditions of valueunbound are as follows:
When session. invalidate () is executed.
The session times out and is automatically destroyed.
Run session. setattribute ("onlineuserlistener", "other objects"); or session. removeattribute ("onlineuserlistener") to delete the listener from the session.

Therefore, as long as the listener is not deleted from the session, the session can be destroyed.

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.