这里我用的是 ssh框架
1 创建 listener 类
package com.gaeainfo.toponymbase.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class initToponymbaseListener implements ServletContextListener{
/**
*结束的时候
*/
public void contextDestroyed(ServletContextEvent arg0) {
}
/**
* 初始化的时候构建map
*/
public void contextInitialized(ServletContextEvent arg0) {
mapBuilder.initMap();
}
}
2 构造一个map
package com.gaeainfo.toponymbase.listener;
import java.util.HashMap;
import java.util.Map;
public class mapBuilder {
public static Map renMap = new HashMap();
public static void initMap(){
System.out.println("正在构建政区map!");
renMap.put("320505", "区号");
}
}
3 .web.xml中配置的修改
<listener> <listener-class>com.gaeainfo.toponymbase.listener.initToponymbaseListener</listener-class> </listener>
4.获取map中的值
ActionContext ac = ActionContext.getContext();
HttpServletRequest request =(HttpServletRequest)ac.get(ServletActionContext.HTTP_REQUEST);
String rename = mapBuilder.renMap.get(numberRen);