Web project management web.xml Listener monitoring summary

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:



org.springframework.web . context.ContextLoaderListener




<listener-class&> com.brand.common . listener.LogoutListener&lt ;/listener-class&>


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) {



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.clear ();


event.getSession ().getServletContext().setAttribute(“loginUserMap”,loginUserMap);



2. Implementation of ServletContextAttributeListener listening example, read fdfs.properties Configuration file

web.xml Configuration:


& lt; Description &> initialize resource information & lt; & Description & gt

<listener-class&> com.brand.common . listener.InitEnvListener&lt ;/listener-class&>


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 {


private WebApplicationContext springContext;

public void contextDestroyed(ServletContextEvent evt) {


public void contextInitialized(ServletContextEvent evt) {


FileInputStream in =null;

InputStreamReader inreader=null;


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



inreader.close ();



}catch(Exception e){





Similar Posts: