jsp中应用Aplication统计访问量
生活随笔
收集整理的這篇文章主要介紹了
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.缺點:每次的訪問都要讀取數據與更新數據,效率低,速度慢,消耗內存。
服務器啟動后就產生了這個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中应用Aplication统计访问量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos查找和替换字符串
- 下一篇: Kafka实战 - 06 Kafka消费