日期:2014-05-18 浏览次数:20668 次
public class CreateServletContext implements ServletContextListener {
private static ServletContext servletContext = null ;
public ServletContext getServletContext() {
return servletContext;
}
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("init and create ServletContext");
if( null == CreateServletContext.servletContext )
CreateServletContext.servletContext = sce.getServletContext();
..........无关紧要的就懒得贴了...........
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
..........无关紧要的就懒得贴了...........
}
}
public class GetServletContext {
private static ServletContext servletContext = null ;
public static ServletContext getServletContext(){
if( null == servletContext ){
CreateServletContext createServletContext = new CreateServletContext();
servletContext
= createServletContext.getServletContext();
}
servletContext.setAttribute("userNameRecord", new HashMap<String,Object>());
return servletContext ;
}
}
private void _prepareSession(UserInf userInf,HttpServletRequest req){
..........无关紧要的就懒得贴了...........
ServletContext sc = GetServletContext.getServletContext();/**获取ServletContext*/
HashMap<String,Object> userNameRecord = (HashMap<String,Object>)sc.getAttribute("userNameRecord");
userNameRecord.put(userInf.getUserName(), userInf.getUserName());
ServletContext ss = GetServletContext.getServletContext();
HashMap<String,Object> record = (HashMap<String,Object>)ss.getAttribute("userNameRecord");
System.out.println("userNameRecord="+userNameRecord+"record == "+record);
}