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页面中。

posted @ 2016-10-11 15:43  wuyongwork  阅读(3014)  评论(0编辑  收藏  举报