J2EE监听器和J2SE事件处理中监听器一样,用于监视事件动作。
J2EE监听器类别:
请求监听器:
ServletRequestListener、
ServletRequestAttributeListener
HttpSession监听器:
HttpSessionListener、
HttpSessionAttributeListener
Servlet上下文监听器:
ServletContextListener、
ServletContextAttributeListener
上下文监听器相关方法:
ServletContextListener
方法 |
说明 |
contextInitialized(ServletContextEvent sce) |
WEB应用准备处理请求时触发 |
contextDestroyed(ServletContextEvent sce) |
关闭时触发 |
ServletContextAttributeListener
方法 |
说明 |
attributeAdded(ServletContextAttributeEvent scab) |
新增一个属性时触发 |
attributeRemoved(ServletContextAttributeEvent scab) |
删除一个属性时触发 |
attributeReplaced(ServletContextAttributeEvent scab) |
替换属性值时触发 |
HttpSession监听器相关方法:
HttpSessionListener
方法 |
说明 |
sessionCreated(HttpSessionEvent se) |
Session创建时触发 |
sessionDestroyed(HttpSessionEvent se) |
Session关闭时触发 |
HttpSessionAttributeListener
方法 |
说明 |
attributeAdded(HttpSessionBindingEvent hsbe) |
新增一个属性时触发 |
attributeRemoved(HttpSessionBindingEvent hsbe) |
删除一个属性时触发 |
attributeReplaced(HttpSessionBindingEvent hsbe) |
替换属性值时触发 |
请求监听器相关方法:
ServletRequestListener
方法 |
说明 |
sessionCreated(HttpSessionEvent se) |
Session创建时触发 |
sessionDestroyed(HttpSessionEvent se) |
Session关闭时触发 |
ServletRequestAttributeListener
方法 |
说明 |
attributeAdded(HttpSessionBindingEvent srae) |
新增一个属性时触发 |
attributeRemoved(HttpSessionBindingEvent srae) |
删除一个属性时触发 |
attributeReplaced(HttpSessionBindingEvent srae) |
替换属性值时触发 |
需要在服务器启动时就加载就必须用上下文监听器
监听器(部署运行)
<listener>
<listener-class>
com.fjdz.listener.InitListener
</listener-class>
</listener>
监听器(开发):实现ServletContextListener的接口
以下是实现服务器启动时从配置文件中读出需要从数据文件要读取的相应数据,并取出数据保存在内存中
package com.fjdz.listener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.fjdz.getdata.beans.DataBean;
import com.fjdz.getdata.query.QueryFactory;
public class InitListener implements ServletContextListener {
private Element root = null;
private Document doc = null;
public static List<List<DataBean>> list;
public static List<List<DataBean>> getList() {
return list;
}
public static void setList(List<List<DataBean>> list) {
InitListener.list = list;
}
public void contextDestroyed(ServletContextEvent sce) {
}
@SuppressWarnings("unchecked")
public void contextInitialized(ServletContextEvent sce) {
SAXReader reader = new SAXReader();
List<List<DataBean>> datalist = new ArrayList<List<DataBean>>();
try {
doc = reader.read(new File(sce.getServletContext().getRealPath("WEB-INF") + "/xml/datainfo.xml"));//用DOM-4j读取配置文件
root = doc.getRootElement();
List rootList = root.selectNodes("/datas/data");//使用X-Path读取根节点
QueryFactory queryFactory = new QueryFactory();
EngineInitializer.initializeEngine();
AoInitialize aoInitialize = new AoInitialize();
aoInitialize.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
for(Object obj : rootList){
List<DataBean> beanlist = new ArrayList<DataBean>();
List<DataBean> blist = new ArrayList<DataBean>();
Element element = (Element)obj;
String dataType = element.attributeValue("datatype");//读取子节点下的属性值
String path = element.attributeValue("path");
List rootList1 = root.selectNodes("/datas/data/trees");
for(Object o : rootList1){
Element element1 = (Element)o;
String className = element1.attributeValue("classname");
String code = element1.attributeValue("code");
String name = element1.attributeValue("name");
beanlist = queryFactory.query(dataType, className, path, code, name,"");//读取数据的方法
datalist.add(beanlist);
}
List rootList2 = root.selectNodes("/datas/data/trees/tree");
for(Object object : rootList2){
Element element2 = (Element)object;
String className = element2.attributeValue("classname");
String code1 = element2.attributeValue("code");
String name1 = element2.attributeValue("name");
String keycode = element2.attributeValue("keycode");
blist = queryFactory.query(dataType, className, path, code1, name1,keycode);
datalist.add(blist);
}
}
aoInitialize.shutdown();
InitListener.setList(datalist);//数据保存在静态变量中存放于内存里
} catch (IOException e) {
e.printStackTrace();
}catch (DocumentException e) {
e.printStackTrace();
}
}
}