14.ServletContext存储全局数据
14.1 原理
ServletContext是一个全局共享区,服务启动后产生,服务停止后销毁,用户可以把共享数据存在ServletContext中,这时候在任何一个Action
中都可以直接使用,主要功能是好像一个共享Map;
14.2 存入:
ServletContext app = request.getSession(true).getServletContext();
Map<String , String> map = new HashMap<String, String>();
map.put("name", "Kevinb");
map.put("sex", "M");
app.setAttribute("map", map);
14.3 取出:
ServletContext app = request.getSession(true).getServletContext();
Map<String, String> map = (Map<String, String>)app.getAttribute("map");
String name = map.get("name");
String sex = map.get("sex");