Servlet三大域对象的应用 request、session、application(ServletContext)
ServletContext是一个全局的储存信息的空间,服务器开始就存在,服务器关闭才释放。
request,一个用户可有多个;session,一个用户一个;而servletContext,所有用户共用一个。所以,为了节省空间,提高效率,ServletContext中,要放必须的、重要的、所有用户需要共享的线程又是安全的一些信息。
ServletContext servletContext = arg0.getServletContext();
servletContext.setAttribute("users", users);
servletContext.setAttribute("online", online);
ServletContext是application内置对象的类型,application可以取出对应key下的值
Set<String> online = (Set<String>)application.getAttribute("online");