每日博客

application对象实现网站计数器

 <%
 Object oCount = application.getAttribute("count");   //取得count属性值,如果还没有改属性则返回null;
 Integer count=null;
 if(oCount==null){
  count = new Integer(1);   //新建一个Integer对象,因为这是首个访问者,所以包含值1
  application.setAttribute("count", count);  //为application对象增加count属性并制定其值;
 }else{
  count = (Integer)application.getAttribute("count");
  count = new Integer(count.intValue()+1);  //将count对象包含的值增加1
  application.setAttribute("count", count); //把增加后的新count对象赋值到application对象的count属性
 }
 %>
 <br>
<font color="red"><%=count.intValue() %></font>

 

posted @ 2021-06-07 21:01  谦寻  阅读(35)  评论(0编辑  收藏  举报