jsp中应用Aplication统计访问量

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.缺点:每次的访问都要读取数据与更新数据,效率低,速度慢,消耗内存。

你可能感兴趣的:(jsp,应用服务器,浏览器,db2)