13, Monitor

1, Monitor

1.1. Function of monitor

	+++What is a monitor?
		
		Listener: it is mainly used to listen to the creation or destruction of specific objects and the change of properties!
			     Is a common java class that implements a specific interface!
	
	+++What objects need to be monitored?
	
		Participants:
			Create your own (no listening)
			Others create their own (need to monitor)
	
		Which objects in the Servlet need listening?
			request / session / servletContext
			Corresponding to the request listener, session related listener and servletContext listener respectively

1.2 classification of monitors

	Listener interface (eight interfaces):
	
	1, Lifecycle listener (listener interface for listener creation / destruction)
			
			Interface ServletRequestListener listens for the creation or destruction of a request object
			Interface HttpSessionListener listens for the creation or destruction of session objects
			Interface ServletContextListener listens for creation or destruction of servletContext objects

	2, Property listener (listens for changes in object properties)
	
			Interface ServletRequestAttributeListener listens for request object attribute changes: add, remove, modify
			Interface HttpSessionAttributeListener listens for session object attribute changes: add, remove, modify
			Interface ServletContextAttributeListener listens for property changes of servletContext object
	
	3, session dependent listener
		Interface HttpSessionBindingListener listens for events when an object is bound to a session	
	    Interface HttpSessionActivationListener (understand) listens for session serialization and deserialization events

404 (wrong path)
500 (server error, debug)
1.2 life cycle monitor
Declaration cycle listener: the process of creating and destroying listening objects!
Development steps of listener:
1. Write a common java class to implement the relevant interface;
2. Configuration (web.xml)

2, Life cycle monitor

2.1 ServletRequestListener (request object listener)

		
		
		|-- ServletRequestListener:		
					requestInitialized(ServletRequestEvent arg0)  
										This method is called when the request object is created.
										
				    requestDestroyed(ServletRequestEvent arg0)
				    					This method is called when the request object is destroyed.
							  
		1.ServletRequestListener is a request object listener. Listen for the creation and destruction of request objects.
		
		2. Execution time:
		
			The user sends an http request,
					The request object is created, triggering the listener's requestInitialized ()
			After the server responds to the data,
					The request object is destroyed, triggering the listener's request destroyed ()		
		
					          
					          

MyServletReqestListener.java

/**
 * Listen to the creation and destruction of request object
 * 
 * @author BGS
 *
 */
public class MyServletReqestListener implements ServletRequestListener {
	
	/**
	 * request Called on object destruction
	 */
	@Override
	public void requestDestroyed(ServletRequestEvent sr) {
		
		String msg=(String)sr.getServletRequest().getAttribute("cn");
		System.out.println(msg);
		
		System.out.println("MyServletReqestListener--requestDestroyed");
	}
	
	/**
	 * http Called on object creation
	 */
	@Override
	public void requestInitialized(ServletRequestEvent arg0) {
		
		System.out.println("MyServletReqestListener--requestInitialized");
	}

}

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	
	<%request.setAttribute("cn", "china"); %>

</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <!-- Monitor request Creation and destruction of objects -->
  <listener>
  			<listener-class>org.jsoft.filter.MyServletReqestListener</listener-class>
  			
  </listener>
  

</web-app>

Visit index.jsp.
The listener listens for the creation of a request object. Trigger the requestInitialized method.

2.2 ServletContextListener (servletContext object listener)

		
		|-- ServletContextListener:		
					contextInitialized(ServletContextEvent sce)
										This method is called when the servletContext object is created.
										
				    contextDestroyed(ServletContextEvent sce)
				    					This method is called when the servletContext object is destroyed.
							  
		1.ServletContextListener is a servletContext object listener. Listen for the creation and destruction of servletContext objects.
		
		2. Execution time:
		
			When the user starts the project,
					The ServletContext object is created, triggering the listener's contextInitialized()
			When the user closes the project,
					The ServletContext object is destroyed, triggering the listener's contextDestroyed()		
			
					          
					          

MyServletContextListener .java

/**
 * Creation and destruction of listener servletContext object
 * 
 * @author BGS
 *
 */
public class MyServletContextListener  implements ServletContextListener{
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		String str=(String) sce.getServletContext().getAttribute("cn");
		System.out.println(str);
		System.out.println("MyServletContextListener--contextInitialized");		
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("MyServletContextListener--contextDestroyed");
	}



}


index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	
	<%application.setAttribute("cn", "china"); %>

</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <!-- Monitor request Creation and destruction of objects -->
  <listener>
  			<listener-class>org.jsoft.filter.MyServletReqestListener</listener-class>
  			
  </listener>
  
  <!-- Monitor servletContext Creation and destruction of objects -->
  <listener>
  			<listener-class>org.jsoft.filter.MyServletContextListener</listener-class>		
  </listener>

</web-app>

Start the tomcat server.
Start the tomcat server, the ServletContext object will be created, and the listener's start method will be triggered.
When the tomcat server is shut down, the ServletContext object is destroyed, and the listener's end method is triggered.

2.3 HttpSessionListener (HttpSession object listener)

		
		|-- HttpSessionListener:		
					sessionCreated(HttpSessionEvent arg0)
										This method is called when the HttpSession object is created.
										
				   sessionDestroyed(HttpSessionEvent arg0)
				    					This method is called when the HttpSession object is destroyed.
							  
		1.HttpSessionListener is the HttpSession object listener. Listen for the creation and destruction of HttpSession objects.
		
		2. Execution time:
				1) jsp file contains nine built-in objects, and session will be created automatically.
				   
				   When the user requests the jsp file, the jsp file will automatically create the object, and the sesson object will also be created.
				   The session object listener is triggered.
				
				2) The session object listener is triggered when the user creates a session.

					          
					          

MyHttpSessionListener.java

/**
 * Listen for the creation and destruction of HttpSession object
 * 
 * @author BGS
 *
 */
public class MyHttpSessionListener  implements HttpSessionListener{

	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
		HttpSession session = arg0.getSession();
		System.out.println("MyHttpSessionListener--sessionCreated");		
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
		System.out.println("MyHttpSessionListener--sessionDestroyed");		
	}
	
	


}



index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	
	<%application.setAttribute("cn", "china"); %>

</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <!-- Monitor request Creation and destruction of objects -->
  <listener>
  			<listener-class>org.jsoft.filter.MyServletReqestListener</listener-class>
  			
  </listener>
  
  <!-- Monitor servletContext Creation and destruction of objects -->
  <listener>
  			<listener-class>org.jsoft.filter.MyServletContextListener</listener-class>		
  </listener>
   
   <!-- Monitor HttpSession Creation and destruction of objects -->
  <listener>
  			<listener-class>org.jsoft.filter.MyHttpSessionListener</listener-class>		
  </listener>
  
</web-app>

The user accesses index.jjsp. jsp will automatically create built-in objects, including session objects.
At this point, the session object listener is triggered.

3, Property cycle listener

3.1 ServletRequestAttributeListener

		
		|-- ServletRequestAttributeListener:		
					 attributeAdded(ServletRequestAttributeEvent srae) 
					 				Triggered when a property is added (to get the added property value)
													
				     attributeRemoved(ServletRequestAttributeEvent srae)
				    				Triggered when a property is removed (to get the value of the removed property)
					
					attributeReplaced(ServletRequestAttributeEvent srae)
		  							Triggered when a property is replaced (you can get the property before replacement and the property value after replacement)
		  							
		1.ServletRequestAttributeListener is the listener of the request object attribute. Listen for changes in the properties of the request object.
		
					          
/**
 * Listen for changes in the properties of the request object
 * 
 * @author BGS
 *
 */
public class MyServletReqestAttributeListener implements ServletRequestAttributeListener {
	
	/**
	 * Triggered when a new attribute is added
	 */
	@Override
	public void attributeAdded(ServletRequestAttributeEvent srae) {
			String str=(String) srae.getServletRequest().getAttribute("cn");
			
			System.out.println("Get added properties:"+str);
			
	}
	
	/**
	 * Triggered when a property is removed
	 */
	@Override
	public void attributeRemoved(ServletRequestAttributeEvent srae) {
		String str=(String) srae.getValue();
		System.out.println("Get removed properties:"+str);
	}
	
	/**
	 * Triggered when an attribute is replaced
	 */
	@Override
	public void attributeReplaced(ServletRequestAttributeEvent srae) {
		
		
		//Get the property value before replacement
		Object value = srae.getValue();
		System.out.println("Get properties before replacement:"+value);
		
		//Get the replaced property value
		String str=(String) srae.getServletRequest().getAttribute("cn");
		System.out.println("Get replaced properties:"+str);
	}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <!-- Monitor request Changes in object properties -->
  <listener>
  			<listener-class>org.jsoft.filter.MyServletReqestAttributeListener</listener-class>
  			
  </listener>
  


</web-app>

3.2 ServletContextAttributeListener (listens for changes in attributes of servletContext object)

		
		|-- ServletContextAttributeListener:		
					 attributeAdded(ServletContextAttributeEvent arg0)
					 				Triggered when a property is added (to get the added property value)
													
				     attributeRemoved(ServletContextAttributeEvent arg0)
				    				Triggered when a property is removed (to get the value of the removed property)
					
					 attributeReplaced(ServletContextAttributeEvent arg0) 
		  							Triggered when a property is replaced (you can get the property before replacement and the property value after replacement)
		  							
		1.ServletContextAttributeListener is the listener of servletContext object attribute. Listen for changes in the properties of the servletContext object.
		
					          
/**
 * Listen for changes in servletContext properties
 * 
 * @author BGS
 *
 */
public class MyServletContextAttributeListener implements ServletContextAttributeListener {
	
	//Triggered when a new attribute is added
	@Override
	public void attributeAdded(ServletContextAttributeEvent arg0) {
			Object str = arg0.getServletContext().getAttribute("cn");
			System.out.println("Get new properties:"+str);
	}
	
	//Triggered when a property is removed
	@Override
	public void attributeRemoved(ServletContextAttributeEvent arg0) {
		Object str = arg0.getValue();
		System.out.println("Get removed properties:"+str);		
	}
	
	//Triggered when an attribute is replaced
	@Override
	public void attributeReplaced(ServletContextAttributeEvent arg0) {
		
		//Get properties before replacement
		Object value = arg0.getValue();
		System.out.println("Get properties before replacement:"+value);
		
		//Get replaced properties
		Object str = arg0.getServletContext().getAttribute("cn");
		System.out.println("Get replaced properties:"+str);		
	}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <!-- Monitor request Changes in object properties -->
  <listener>
  			<listener-class>org.jsoft.filter.MyServletReqestAttributeListener</listener-class>
  			
  </listener>
  
    <!-- Monitor servletContext Changes in object properties -->
  <listener>
  			<listener-class>org.jsoft.filter.MyServletContextAttributeListener</listener-class>
  </listener>
  


</web-app>

index.xml

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	
	<%application.setAttribute("cn", "china");
	application.setAttribute("cn", "china1");
	application.removeAttribute("cn");
	%>
</body>
</html>

3.3 HttpSessionAttributeListener

		
		|-- HttpSessionAttributeListener:		
					 attributeAdded(ServletContextAttributeEvent arg0)
					 				Triggered when a property is added (to get the added property value)
													
				     attributeRemoved(ServletContextAttributeEvent arg0)
				    				Triggered when a property is removed (to get the value of the removed property)
					
					 attributeReplaced(ServletContextAttributeEvent arg0) 
		  							Triggered when a property is replaced (you can get the property before replacement and the property value after replacement)
		  							
		1.HttpSessionAttributeListener is the listener of the session object attribute. Listen for changes in session object properties.
		
					          
/**
 * Listening for changes in session object properties
 * 
 * @author BGS
 *
 */
public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener{
	
	//Triggered when a new attribute is added
	@Override
	public void attributeAdded(HttpSessionBindingEvent arg0) {
		Object attribute = arg0.getSession().getAttribute("cn");
		System.out.println("Get new properties:"+attribute);
	}
	
	//Triggered when a property is removed
	@Override
	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		Object attribute = arg0.getValue();
		System.out.println("Get removed properties:"+attribute);		
	}
	
	//Triggered when an attribute is replaced
	@Override
	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		
		Object attribute = arg0.getValue();
		System.out.println("Get properties before replacement:"+attribute);		
		
		Object attribute2 = arg0.getSession().getAttribute("cn");
		System.out.println("Get properties after replacement:"+attribute2);		
	}
	

	



}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <!-- Monitor request Changes in object properties -->
  <listener>
  			<listener-class>org.jsoft.filter.MyServletReqestAttributeListener</listener-class>
  			
  </listener>
  
    <!-- Monitor servletContext Changes in object properties -->
  <listener>
  			<listener-class>org.jsoft.filter.MyServletContextAttributeListener</listener-class>
  </listener>
  
    <!-- Monitor HttpSession Changes in object properties -->
  <listener>
  			<listener-class>org.jsoft.filter.MyHttpSessionAttributeListener</listener-class>
  </listener>

</web-app>

index.xml

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	
	<%application.setAttribute("cn", "china");
	application.setAttribute("cn", "china1");
	application.removeAttribute("cn");
	%>
</body>
</html>

4, Other listeners (session related listeners)

4.1 HttpSessionBindingListener

			HttpSessionBindingListener :
							Listen for the event of binding / unbinding an object to sesson!
			Steps:
				The entity object implements the interface; put the object into the session object / remove, and the listening code will be triggered.
			
			Effect:
				(online reminder! )
			
			Difference: (from other monitors)
				  Is this session listener different from the above declaration cycle and attribute listeners?
					
					--No more web.xml configuration
  					--Because the listening object is the object created by yourself, not the server object!

Admim.java

/**
 * Entity object binding session
 * 
 * @author BGS
 *
 */
public class Admin  implements HttpSessionBindingListener{
	
	private int id ;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public void valueBound(HttpSessionBindingEvent arg0) {
		System.out.println("The object is placed in the session in");
		
	}
	@Override
	public void valueUnbound(HttpSessionBindingEvent arg0) {
		System.out.println("The object from session Remove from");
	}
	
	
}

index.jsp

<%@page import="org.jsoft.filter.Admin"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	
	<%
	
	//Put objects into session
	session.setAttribute("cn", new Admin());
	
	//Remove objects
	session.removeAttribute("cn");
	
	%>
</body>
</html>

Visit index.jsp.
admin is the entity object bound to sesson.
First, put the admin object into the session, and the valueBound() will be triggered automatically
If the admin object is removed from sesson, valueUnbound() will be triggered automatically

5, Online list display

Online list display items

Published 99 original articles, won praise 0, visited 1105
Private letter follow

Keywords: xml Java Session Attribute

Added by agoe on Sun, 23 Feb 2020 09:57:01 +0200