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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值