application的使用(实现计数器)

application在整个WEB项目中共享使用数据。

常用方法:
 getAttribute();
 setAttribute();
示列:

 <%
    Object count=application.getAttribute("count");
    if(count==null){
     //application中未存放count
     application.setAttribute("count", new Integer(1));
    }else{
     //application中已经存放count
     Integer i=(Integer)count;
     application.setAttribute("count", i.intValue()+1);
    }
    Integer icount=(Integer)application.getAttribute("count");
    out.println("页面被访问了"+icount.intValue()+"次");
    %>

可以直接将此段代码放在需要显示的JSP页面中。

原文地址:https://www.cnblogs.com/fifiyong/p/5949568.html