A good memory is better than a bad writer. 43-javaweb Session Event Listener (7)

Source: Internet
Author: User
Tags event listener

Javaweb application, a lot of places are related to the session. Therefore, session-related event listeners are useful in daily work.
The Servlet specification defines two special listener interfaces "Httpsessionbindinglistener and Httpsessionactivationlistener" to help JavaBean Object to understand these state changes in the session domain, the classes that implement both interfaces do not need to be registered in the Web. xml file.
1. Httpsessionbindinglistener interface
The JavaBean object that implements the Httpsessionbindinglistener interface can perceive events that are bound to the session and deleted by the session.
When an object is bound to a HttpSession object, the Web server calls the object's void Valuebound (Httpsessionbindingevent event) method
When the object is unbound from the HttpSession object, the Web server calls the object's void Valueunbound (Httpsessionbindingevent event) method

2. Httpsessionactivationlistener interface

JavaBean objects that implement the Httpsessionactivationlistener interface can perceive themselves as activated (deserialized) and passivated (serialized) events
When a JavaBean object that is bound to a HttpSession object is about to be passivated (serialized) with the HttpSession object, the Web server calls the JavaBean object's void Sessionwillpassivate ( Httpsessionevent event) method. This allows the JavaBean object to know that it will be serialized (passivated) to the hard disk along with the HttpSession object.
When the JavaBean object that is bound to the HttpSession object is about to be activated (deserialized) with the HttpSession object, the Web server calls the JavaBean object's void Sessiondidactive ( Httpsessionevent event) method. This way the JavaBean object will know that it will be deserialized (activated) back into memory with the HttpSession object

3, JavaBean sense oneself is bound to the session in the source code
The JavaBean object that implements the Httpsessionbindinglistener interface can perceive events that are bound to the session and deleted by the session.
When an object is bound to a HttpSession object, the Web server calls the object's void Valuebound (Httpsessionbindingevent event) method
When the object is unbound from the HttpSession object, the Web server calls the object's void Valueunbound (Httpsessionbindingevent event) method.
The sample source code is as follows:

 PackageCom.servlet.listener;ImportJavax.servlet.http.HttpSessionBindingEvent;ImportJavax.servlet.http.HttpSessionBindingListener;/** * Simple implementation of Httpsessionbindinglistener interface, know if the JavaBean is added to the session. * @author Fan Fangming * * Public  class easylisenterself implements Httpsessionbindinglistener{    PrivateString name; Public easylisenterself(String name) { This. name = name; }@Override     Public void Valuebound(Httpsessionbindingevent event) {System.out.println (name+", join the session. "); }@Override     Public void Valueunbound(Httpsessionbindingevent event) {System.out.println (name+", the session is removed. "); } PublicStringGetName() {returnName }}

4. session.jsp for testing

<%@ page language="java" pageencoding="UTF-8"%><%@ page import= "com.servlet.listener.EasyLisenterSelf" %><! DOCTYPE html><html>  <head>    <title>Simple test</title>  </head>  <body>    <%//Binds the JavaBean object to the session Session.setattribute ("Bean",new easylisenterself ("        JavaBean monitoring itself "));    Removes the JavaBean object Session.removeattribute ("Bean") from the session; %>  </body></html>

5. Operation Result
Start Web middleware, access from URL:
http://127.0.0.1:8080/webStudy/session.jsp
Console print Condition:
JavaBean monitor himself and join the session.
JavaBean Monitor itself, the session is removed.

A good memory is better than a bad writer. 43-javaweb Session Event Listener (7)

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.