1. Listener that listens for changes in properties in domain objects
The event listener for changing attributes in domain objects is the listener for listening to the attribute change information events in the three objects: ServletContext, HttpSession, and HttpServletRequest.
These three listener interfaces are ServletContextAttributeListener, HttpSessionAttributeListener and ServletRequestAttributeListener. All three interfaces define three methods to handle the events that increase, delete and replace attributes in the listened object. The corresponding method names of the same event in these three interfaces are exactly the same, but the accepted parameters are different.
1.1. AttributeAdded method
When adding an attribute to the object being listened, the web container responds by calling the attributeAdded method of the event listener. This method receives an event type parameter. The listener can obtain the domain object that is adding attributes and the attribute objects saved in the domain. The complete syntax definition of each domain attribute listener is as follows:
public void attributeAdded(ServletContextAttributeEvent scae) public void attributeReplaced(HttpSessionBindingEvent hsbe) public void attributeRmoved(ServletRequestAttributeEvent srae)
1.2. AttributeRemoved method
When deleting an attribute in the object being listened, the web container calls the attributeRemoved method of the event listener to respond to the complete syntax definition in the attribute listener in each domain:
public void attributeRemoved(ServletContextAttributeEvent scae)public void attributeRemoved (HttpSessionBindingEvent hsbe)public void attributeRemoved (ServletRequestAttributeEvent srae)
1.3. AttributeReplaced method
When a certain attribute in the listener's domain object is replaced, the web container calls the attributeReplaced method of the event listener to respond to the complete syntax definition in each domain attribute listener:
public void attributeReplaced(ServletContextAttributeEvent scae)public void attributeReplaced (HttpSessionBindingEvent hsbe)public void attributeReplaced (ServletRequestAttributeEvent srae)
1.4. ServletContextAttributeListener listener example:
Write a ServletContextAttributeListener listener to monitor the changes in attribute values of ServletContext domain objects. The code is as follows:
package me.gacl.web.listener;import java.text.MessageFormat;import javax.servlet.ServletContextAttributeEvent;import javax.servlet.ServletContextAttributeListener;/*** @ClassName: MyServletContextAttributeListener* @Description: Event listener for changing attributes in ServletContext domain object* @author: Lonely Canglang* @date: 2014-9-11 10:53:04 pm**/ public class MyServletContextAttributeListener implements ServletContextAttributeListener { @Override public void attributeAdded(ServletContextAttributeEvent scab) { String str =MessageFormat.format( "ServletContext domain object has added attribute: {0}, attribute value is: {1}" ,scab.getName() ,scab.getValue()); System.out.println(str); } @Override public void attributeRemoved(ServletContextAttributeEvent scab) { String str =MessageFormat.format( "Delete attribute: {0} in the ServletContext domain object, the attribute value is: {1}" ,scab.getName() ,scab.getValue()); System.out.println(str); } @Override public void attributeReplaced(ServletContextAttributeEvent scab) { String str =MessageFormat.format( "The value of attribute: {0} was replaced in the ServletContext domain object" ,scab.getName()); System.out.println(str); }}Register the listener in the web.xml file
<listener> <description>MyServletContextAttributeListener Listener</description> <listener-class>me.gacl.web.listener.MyServletContextAttributeListener</listener-class> </listener>
Write ServletContextAttributeListenerTest.jsp test page
<%@ page language="java" pageEncoding="UTF-8"%><!DOCTYPE HTML><html> <head> <title>ServletContextAttributeListener Listener Test</title> </head> <body> <% //Add attribute application.setAttribute("name", "Longyanglang"); //Replace the value of the name attribute in the application domain object application.setAttribute("name", "gacl"); //Remove the name attribute in the application domain object application.removeAttribute("name"); %> </body></html>The operation results are as follows:
From the running results, we can see that the ServletContextListener listener successfully listened to the change of the property value in the ServletContext domain object (application).
1.5. ServletRequestAttributeListener and HttpSessionAttributeListener listener example:
Write a listener to monitor the changes in attribute values of HttpSession and HttpServletRequest domain objects, the code is as follows:
package me.gacl.web.listener;import java.text.MessageFormat;import javax.servlet.ServletRequestAttributeEvent;import javax.servlet.ServletRequestAttributeListener;import javax.servlet.http.HttpSessionAttributeListener;import javax.servlet.http.HttpSessionBindingEvent;public class MyRequestAndSessionAttributeListener implements HttpSessionAttributeListener, ServletRequestAttributeListener { @Override public void attributeAdded(ServletRequestAttributeEvent srae) { String str =MessageFormat.format( "ServletRequest domain object has added attribute: {0}, attribute value is: {1}" ,srae.getName() ,srae.getValue()); System.out.println(str); } @Override public void attributeRemoved(ServletRequestAttributeEvent srae) { String str =MessageFormat.format( "Remove attributes in the ServletRequest domain object: {0}, the attribute value is: {1}",srae.getName(),srae.getValue()); System.out.println(str); } @Override public void attributeReplaced(ServletRequestAttributeEvent srae) { String str =MessageFormat.format( "The value of attributes: {0} was replaced in the ServletRequest domain object" ,srae.getName()); System.out.println(str); } @Override public void attributeAdded(HttpSessionBindingEvent se) { String str =MessageFormat.format( "The attribute: {0} was added to the "HttpSession domain object, the attribute value is: {1}" ,se.getName() ,se.getValue()); System.out.println(str); } @Override public void attributeRemoved(HttpSessionBindingEvent se) { String str =MessageFormat.format( "The attribute: {0} was deleted from the "HttpSession domain object, the attribute value is: {1}" ,se.getName() ,se.getValue()); System.out.println(str); } @Override public void attributeReplaced(HttpSessionBindingEvent se) { String str =MessageFormat.format( "The value of attribute:{0} was replaced in the HttpSession domain object" ,se.getName()); System.out.println(str); }}Register the listener in the web.xml file
<listener> <description>MyRequestAndSessionAttributeListener Listener</description> <listener-class>me.gacl.web.listener.MyRequestAndSessionAttributeListener</listener-class> </listener>
Write RequestAndSessionAttributeListenerTest.jsp test page
<%@ page language="java" pageEncoding="UTF-8"%><!DOCTYPE HTML><html> <head> <title>RequestAndSessionAttributeListener Listener Test</title> </head> <body> <% //Add attribute session.setAttribute("aa", "bb"); //Replace the value of the aa attribute in the session domain object session.setAttribute("aa", "xx"); //Remove the aa attribute session.removeAttribute("aa"); //Add attribute request.setAttribute("aa", "bb"); //Replace the value of the aa attribute in the request domain object request.setAttribute("aa", "xx"); //Remove the aa attribute request.removeAttribute("aa"); %> </body></html>The operation results are as follows:
From the run results, we can see that the HttpSessionAttributeListener listener and ServletRequestAttributeListener successfully listened to the changes in the attribute values of the HttpSession domain object and the HttpServletRequest domain object.
2. Aware of the event listener bound to Session
Objects stored in the Session domain can have multiple states: binding (session.setAttribute("bean", Object)) into the Session; unbinding (session.removeAttribute("bean")) from the Session domain; persisting to a storage device with the Session object; recovering from a storage device with the Session object. Two special listener interfaces "HttpSessionBindingListener and HttpSessionActivationListener" are defined in the Servlet specification to help JavaBean objects understand their states in the Session domain: , classes that implement these two interfaces do not require registration in the web.xml file.
2.1. HttpSessionBindingListener interface
JavaBean object that implements the HttpSessionBindingListener interface can sense that it is bound to the Session and deleted events in the Session. When the object is bound to the HttpSession object, the web server calls the void valueBound(HttpSessionBindingEvent event) method of the object. When the object is unbound from the HttpSession object, the web server calls the void valueUnbound(HttpSessionBindingEvent event) method of the object.
example:
package me.gacl.domain;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionBindingListener;/*** @ClassName: JavaBeanDemo1* @Description: * The JavaBean object that implements the HttpSessionBindingListener interface can sense that it is bound to the Session and deleted events from the Session When the object is bound to the HttpSession object, the web server calls the void valueBound(HttpSessionBindingEvent event) method of the object when the object is bound to the HttpSession object. When unbinding in the object, the web server calls the object's void valueUnbound(HttpSessionBindingEvent event) method* @author: Lonely Canglang* @date: 2014-9-11 11:14:54 pm**/ public class JavaBeanDemo1 implements HttpSessionBindingListener { private String name; @Override public void valueBound(HttpSessionBindingEvent event) { System.out.println(name+" was added to session"); } @Override public void valueUnbound(HttpSessionBindingEvent event) { System.out.println(name+"Kicked out by session"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public JavaBeanDemo1(String name) { this.name = name; }}The above JavaBeanDemo1 javabean implements the HttpSessionBindingListener interface, so this JavaBean object can sense that it is bound to the Session and deleted from the Session. The test code is as follows:
<%@ page language="java" pageEncoding="UTF-8"%><%@page import="me.gacl.domain.JavaBeanDemo1"%><!DOCTYPE HTML><html> <head> <title></title> </head> <body> <% //Bind the javabean object to the session.setAttribute("bean",new JavaBeanDemo1("Lonely Canglang")); //RemoveAttribute("bean"); %> </body></html>The operation results are as follows:
2.2. HttpSessionActivationListener interface
The JavaBean object that implements the HttpSessionActivationListener interface can sense itself to be activated (deserialized) and passivated (serialized) events. Before the javabean object bound to the HttpSession object will be passivated (serialized) with the HttpSession object, the web server calls the void sessionWillPassivate(HttpSessionEvent event) method of the javabean object. In this way, the javabean object can know that it will be serialized (passive) to the hard disk with the HttpSession object.
After the javabean object bound to the HttpSession object is to be activated (deserialized) with the HttpSession object, the web server calls the void sessionDidActive(HttpSessionEvent event) method of the javabean object. In this way, the javabean object can know that it will be deserialized (activated) and returned to memory with the HttpSession object.
example:
package me.gacl.domain;import java.io.Serializable;import javax.servlet.http.HttpSessionActivationListener;import javax.servlet.http.HttpSessionEvent;/*** @ClassName: JavaBeanDemo2* @Description: JavaBean object that implements the HttpSessionActivationListener interface can perceive the activation of the event that it is activated and passive: the javabean object and the Session are deserialized (activated) into memory together. Passivation: The javabean object exists in the Session. When the server serializes the session to the hard disk, if the javabean object in the Session implements the Serializable interface, the server will serialize the javabean object in the Session to the hard disk together. The operation of the javabean object and the Session to the hard disk together is called passivation. If the javabean object in the Session does not implement the Serializable interface, the server will first remove the javabean object in the Session that does not implement the Serializable interface and then serialize (passivate) the Session to the hard disk. When the javabean object bound to the HttpSession object will be passivated with the HttpSession object, the web server calls the void of the javabean object object before the javabean object bound to the HttpSession object is passivated with the HttpSession object. sessionWillPassivate(HttpSessionEvent event) method so that the javabean object can know that it will be serialized (passive) with the HttpSession object to the hard disk. After the javabean object bound to the HttpSession object will be activated with the HttpSession object, the web server calls the void sessionDidActive(HttpSessionEvent event) method of the javabean object so that the javabean object can know that it will be deserialized (reactivated) with the HttpSession object back into memory* @author: Guarante* @date: 2014-9-11 11:22:35 pm**/ public class JavaBeanDemo2 implements HttpSessionActivationListener, Serializable { private static final long serialVersionUID = 7589841135210272124L; private String name; @Override public void sessionWillPassivate(HttpSessionEvent se) { System.out.println(name+" and session are serialized (passive) to the hard disk together. The id of the session is: "+se.getSession().getId()); } @Override public void sessionDidActivate(HttpSessionEvent se) { System.out.println(name+" and session are deserialized (activated) from the hard disk together. The id of the session is: "+se.getSession().getId()); } public String getName() { return name; } public void setName(String name) { this.name = name; } public JavaBeanDemo2(String name) { this.name = name; }}In order to observe the process of the javabean object bound to the HttpSession object being passivated to the hard disk and reactivated from the hard disk to the memory, we need to use the tomcat server to help us complete the passivation and activation process of the HttpSession object. The specific methods are as follows:
Create a context.xml file in the WebRoot/META-INF folder as follows:
The contents of the context.xml file are as follows:
<Context> <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1"> <Store className="org.apache.catalina.session.FileStore" directory="gacl"/> </Manager> </Context>
After 1 minute of configuration in the context.xml file, passivate the HttpSession object into a gacl folder on the local hard disk
The jsp test code is as follows:
<%@ page language="java" pageEncoding="UTF-8"%><%@page import="me.gacl.domain.JavaBeanDemo2"%><!DOCTYPE HTML><html> <head> <title></title> </head> <body> As soon as you access the JSP page, HttpSession is created. The ID of the created Session is: ${pageContext.session.id} <hr/> <% session.setAttribute("bean",new JavaBeanDemo2("Lonely and Proudly")); %> </body></html>When accessing this jsp page, the server will immediately create an HttpSession object, and then bind the JavaBean object that implements the HttpSessionActivationListener interface to the session object. After waiting for 1 minute, no one will access this jsp page again, and the server will automatically passivate (serialize) the HttpSession object to the hard disk.
We can find the session serialized to local storage in the tomcat server's work/Catalina/localhost/JavaWeb_Listener_20140908/gacl folder, as shown in the figure below:
When accessing this Jsp page again, the server will automatically reactivate (deserialize) the HttpSession object on the hard disk and return it to memory. The operation results are as follows:
There is only so much content in the listener technology in JavaWeb development technology. In daily work, listener technology is used more frequently in JavaWeb project development, so you must master this technology.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.