Listener for properties in listening domain object
The event listener of property change in domain object is used to listen for property change information events in ServletContext, httpsession and HttpServletRequest.
The three listener interfaces are ServletContextAttributeListener, HttpSessionAttributeListener and ServletRequestAttributeListener have defined three methods to handle the events of adding, deleting and replacing attributes in the monitored object. The corresponding method names of the same event in the three interfaces are exactly the same, but the parameter types are different.
The first thing to be sure is that the loading order is related to them web.xml There is no order in the file. That is, the filter will not be loaded first because it is written in front of the listener. The final conclusion is: listener – &> filter – &> servlet
At the same time, there is such a configuration section: context param, which is used to provide ServletContext with key value pairs, that is, application context information. Our listeners and filters use the information in these contexts during initialization. Should the context param configuration section be written before the listener configuration section?In fact, the context param configuration section can be written anywhere, so the real loading order of is: context param – &> listener – &> filter – &> servlet
For a certain type of configuration section, it is related to the order in which they appear. Take filter as an example, web.xml Of course, multiple filters can be defined in. A configuration section related to filter is filter mapping. Here, it must be noted that for filter and filter mapping configuration sections with the same filter name, filter mapping must appear after filter. Otherwise, when resolving to filter mapping, its corresponding filter name will be changed Not yet defined. When the web container is started, each filter is initialized according to the order in which the filter configuration section appears. When the request resource matches multiple filter mapping, the filter intercepting resource calls the dofilter() method in turn according to the order in which the filter mapping configuration section appears.
The servlet is similar to the filter , which will not be repeated here.
From this, we can see that, web.xml The order of loading is: context param – &> listener – &> filter – &> servlet , and the order of actual program calls between the same type is based on the corresponding mapping order.
An example of implementing HttpSessionAttributeListener
web.xml Medium configuration:
<listener&>
<listener-class&>
org.springframework.web . context.ContextLoaderListener
</listener-class&>
</listener&>
<listener&>
<listener-class&> com.brand.common . listener.LogoutListener< ;/listener-class&>
</listener&>
LogoutListener.java Program code:
package com.brand.common .listener;
import java.util.Map ;
import javax.servlet.http .HttpSessionEvent;
import javax.servlet.http .HttpSessionListener;
import com.tugou.bean .UserInofrmationBean;
public class LogoutListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
}
@SuppressWarnings(“unchecked”)
public void sessionDestroyed(HttpSessionEvent event) {
//when the session is destroyed, clear the key value pairs saved in loginusermap
UserInofrmationBean user = (UserInofrmationBean) event.getSession ()
.getAttribute( UserInofrmationBean.USER_ FRONT_ SESSION);
Map<String, String&> loginUserMap = (Map<String, String&>) event.getSession ().getServletContext().getAttribute(
“loginUserMap”);
if(loginUserMap!=null){
loginUserMap.clear ();
}
event.getSession ().getServletContext().setAttribute(“loginUserMap”,loginUserMap);
}
}
2. Implementation of ServletContextAttributeListener listening example, read fdfs.properties Configuration file
0
web.xml Configuration:
<listener&>
& lt; Description &> initialize resource information & lt; & Description & gt
<listener-class&> com.brand.common . listener.InitEnvListener< ;/listener-class&>
</listener&>
InitEnvListener.java Program code:
package com.brand.common .listener;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.InputStreamReader ;
import java.util.Properties ;
import javax.servlet.ServletContextEvent ;
import javax.servlet.ServletContextListener ;
import org.springframework.web . context.WebApplicationContext ;
public class InitEnvListener implements ServletContextListener {
@SuppressWarnings(“unused”)
private WebApplicationContext springContext;
public void contextDestroyed(ServletContextEvent evt) {
}
public void contextInitialized(ServletContextEvent evt) {
try{
FileInputStream in =null;
InputStreamReader inreader=null;
try{
Properties props = new Properties();
File fdfsConfig=new File( Thread.currentThread ().getContextClassLoader().getResource(” fdfs.properties “).getPath());
in= new FileInputStream(fdfsConfig);
inreader=new InputStreamReader(in,”utf-8″);
props.load (inreader);
String imagerootpath = props.getProperty (“imagerootpath”);
evt.getServletContext ().setAttribute(” fdfs.imagerootpath “, imagerootpath);// use application when fetching on the page
}finally{
if(inreader!=null){
inreader.close ();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}