六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 87|回复: 0

jsp中应用Aplication统计访问量

[复制链接]

升级  62%

7

主题

7

主题

7

主题

童生

Rank: 1

积分
31
 楼主| 发表于 2013-1-13 18:38:35 | 显示全部楼层 |阅读模式
1.application的概述
服务器启动后就产生了这个application对象,当客户在所访问的网站的各个页面之间浏览时,这个application对象都是同一个,直到服务器关闭。但是与session不同的是,所有客户的application对象都是同一个,即所有客户共享这个内置的application对象。
2.用到的方法有:
public void setAttribute(String name,Object obj): 设置由name指定的名字的application对象的属性的值object.
public Object getAttribute(String name):返回由name指定的名字的application对象的属性的值.  
3.在jsp中使用Aplication的对象统计访问量
<%   
        Integer   totalCount=(Integer)application.getAttribute("totalCount");   
        if(totalCount==null){   
        DBConnection db2 = new DBConnection();
String sql2 = "select * from sitevisits";
ResultSet rs2 = db2.executeQuery(sql2);
while (rs2.next()) {
String totalcount = rs2.getString("totalcount");
int sum = Integer.parseInt(totalcount);
totalCount=new   Integer(sum);  
}  
        }else{   
                totalCount=new   Integer(totalCount.intValue()+1);
               DBConnection db3 = new DBConnection();
String sql3 = "update sitevisits set totalcount="+totalCount;
int i = db3.executeUpdate(sql3);
        }   
        application.setAttribute("totalCount",totalCount);   
  %>   
<p>访问率:<%=totalCount%></p>
4.缺点:每次的访问都要读取数据与更新数据,效率低,速度慢,消耗内存。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表