需要深入研究Code

application对象(获取网站的访问量)(2)

2019-06-08  本文已影响478人  淡写回忆

application:提供了关于服务器版本,应用级初始化参数和应用内资源据对路径的方式

application作用域:web容器的生命周期,即多用户登录时的数据可以同时进行访问,如网站的访问量(如果有数据需要在整个web应用程序中进行访问的话使用application中的attribute方法即可)

访问量代码:

if (flag == true){

    Object o = application.getAttribute("count");//获取访问的次数

    if (o == null){ //判断是否为空即为0的情况

        application.setAttribute("count",1);//当访问次数为空时修改为1,存入application中

    }else {

        int count = Integer.parseInt(o.toString());//将获取的object类型的访问次数

        application.setAttribute("count",count + 1);//累加访问的数量

    }

前台显示代码为:

<p align="left">访问量为:<%= application.getAttribute("count")%></p>

显示效果为:

上一篇 下一篇

猜你喜欢

热点阅读