servlet中的几个类--ServletConfig&ServletContext(页面访问量的功能实现)

来源:互联网 发布:ubuntu安装32位运行库 编辑:程序博客网 时间:2024/06/02 03:24

ServletConfig

用于在web.xml中查询init-param中的value和name

//读取servlet中的初始化参数ServletConfig:        //法1、        ServletConfig config=getServletConfig();        String charset=config.getInitParameter("charset");//访问到web.xml中init-param中的param-value        // 法2、        String charset2 = this.getInitParameter("charset");        // 法3. 遍历参数        Enumeration<String> en=this.getInitParameterNames();        while(en.hasMoreElements()){            String key=en.nextElement();            String value=getInitParameter("key");            out.println(key+value);        }

ServletContext

根据服务器启动后,不管哪个用户访问的ServletContext都是一样的,它是整个项目的容器,所有用户公用的,可以放东西给用户公用
服务器重启后就会new一个新的

网站点击量就是ServletContext的应用

ServletContext context = getServletContext();        Integer count = (Integer) context.getAttribute("count");        if (count == null) {            count=1;        } else {            count++;        }        getServletContext().setAttribute("count", count);
阅读全文
0 0
原创粉丝点击