JSP實(shí)現(xiàn)網(wǎng)頁訪問統(tǒng)計(jì)
最近學(xué)習(xí)Jave EE 中的jsp網(wǎng)頁開發(fā),需要實(shí)現(xiàn)網(wǎng)頁訪問量的統(tǒng)計(jì),剛開始不知道如何實(shí)現(xiàn),后來問了一下老師,老師是這樣回答我的:要實(shí)現(xiàn)網(wǎng)頁訪問的統(tǒng)計(jì),你可以利用application對(duì)象來實(shí)現(xiàn),不能用seesion對(duì)象,因?yàn)閟ession是屬于同一個(gè)會(huì)話的,關(guān)掉瀏覽器數(shù)據(jù)就沒有了,而application是在同一瀏覽器下的,只要是同一個(gè)瀏覽器,將數(shù)據(jù)保存在applicaiton對(duì)象中,這樣就可以保證數(shù)據(jù)的不變性。其實(shí)這些我都懂,我只是不知道如何在jsp用代碼實(shí)現(xiàn)。后來我只能上網(wǎng)看看有沒有具體的解決方案,搜了很久,沒有我想要的答案,我想要實(shí)現(xiàn)的只是簡(jiǎn)單的統(tǒng)計(jì),沒有實(shí)現(xiàn)更加復(fù)雜的功能。后來還是在CSDN這里找到了答案,在這里簡(jiǎn)單總結(jié)一下實(shí)現(xiàn)網(wǎng)頁訪問統(tǒng)計(jì)的幾種方法:
1. 利用application對(duì)象進(jìn)行統(tǒng)計(jì),得到的效果是每進(jìn)入一次該網(wǎng)頁就統(tǒng)計(jì)一次。但效果不怎么好,因?yàn)橐话憬y(tǒng)計(jì)網(wǎng)頁訪問量,刷新是不算進(jìn)統(tǒng)計(jì)里的,這里就是這種缺點(diǎn)。
具體實(shí)現(xiàn)是:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<head>
<title>java 計(jì)數(shù)器程序</title>
</head>
<body>
<%
if (application.getAttribute("count") == null) {
application.setAttribute("count", new Integer(0));
}
Integer count = (Integer) application.getAttribute("count");
application
.setAttribute("count", new Integer(count.intValue() + 1));
count = (Integer) application.getAttribute("count");
%>
<center>這是第<%=count.intValue()%>個(gè)訪問者</center>
</body>
</html>
2.為了解決上面的問題,有了另一種方法,就是同時(shí)利用application對(duì)象和session對(duì)象來統(tǒng)計(jì),這種方法的原理是從打開瀏覽器到關(guān)閉瀏覽器算是訪問一次,刷新、返回等操作不算做一次訪問。但還是有缺陷,當(dāng)jsp服務(wù)器從新啟動(dòng)時(shí),數(shù)據(jù)也被清零了。
下面還是具體實(shí)現(xiàn):
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<head>
<title>java 計(jì)數(shù)器程序</title>
</head>
<body>
<%
int n = 0; String counter = (String)application.getAttribute("counter");
if(counter != null){
n = Integer.parseInt(counter);
}
if(session.isNew())
++n;
%>
<center>這是第<%out.print(n);%>個(gè)訪問者</center>
<%
counter = String.valueOf(n);
application.setAttribute("counter", counter);
%>
</body>
</html>
3. 第三種方法是將統(tǒng)計(jì)數(shù)據(jù)存儲(chǔ)在本地的文件當(dāng)中,比如存儲(chǔ)在一個(gè)txt文件當(dāng)中。
這是為了解決重啟服務(wù)器之后數(shù)據(jù)不用擔(dān)心會(huì)丟失。
創(chuàng)建一個(gè)類:JSPCount
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class JSPCount {
//寫入文件的方法
public static void write2File(String filename, long count){
try{
PrintWriter out = new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//讀文件的方法
public static long readFromFile(String filename){
File file = new File(filename);
long count = 0;
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
write2File(filename, 0);
}
try{
BufferedReader in = new BufferedReader(new FileReader(file));
try{
count = Long.parseLong(in.readLine());
}
catch (NumberFormatException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
return count;
}
}
在WebRoot目錄下建jsp文件:count.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page import="org.wwj.count.JSPCount" %>
<html>
<head>
<title>java 計(jì)數(shù)器程序</title>
</head>
<body>
<%
JSPCount CountFileHandler = new JSPCount();
//讀取文件
long count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");
count = count + 1; //修改記錄 +1
out.print(count); //顯示數(shù)據(jù)
//更新文件內(nèi)容。
CountFileHandler.write2File(request.getRealPath("/") + "count.txt", count);
%>
</body>
</html>
程序運(yùn)行之后會(huì)在tomcat下的webapps目錄下的對(duì)應(yīng)的web項(xiàng)目生成一個(gè)count.txt文本文件
4.第四種方法,只是保存了訪問的統(tǒng)計(jì)數(shù)據(jù)罷了,但沒有保證刷新頁面的時(shí)候不會(huì)自增,這樣還是不好。當(dāng)然總會(huì)有解決的辦法的,一般的解決方案就是結(jié)合各種方案的優(yōu)點(diǎn)。下面是由session對(duì)象+application對(duì)象+txt文本來實(shí)現(xiàn)網(wǎng)站的訪問統(tǒng)計(jì)。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
public class Counter extends HttpServlet{
//寫入文件的方法
public static void write2File(String filename, long count){
try{
PrintWriter out = new PrintWriter(new FileWriter(filename));
out.println(count);
out.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
//讀文件的方法
public static long readFromFile(String filename){
File file = new File(filename);
long count = 0;
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
write2File(filename, 0);
}
try{
BufferedReader in = new BufferedReader(new FileReader(file));
try{
count = Long.parseLong(in.readLine());
}
catch (NumberFormatException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
return count;
}
}
jsp文件代碼:
<%@page import="org.servlet.count.Counter"%>
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<head>
<title>java 計(jì)數(shù)器程序</title>
</head>
<body>
<%
Counter CountFileHandler = new Counter();
long count = 0;
if(application.getAttribute("count") == null){
count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");
application.setAttribute("count", new Long(count));
}
count = (Long)application.getAttribute("count");
if(session.isNew()){
count++;
application.setAttribute("count", count);
//更新文件目錄
CountFileHandler.write2File(request.getRealPath("/") + "count.txt",count);
}
%>
訪問人數(shù):<%=count %>
</body>
</html>
以上四種方法,是每一次改進(jìn)才得到的方法,如果要實(shí)現(xiàn)網(wǎng)站訪問統(tǒng)計(jì),當(dāng)然最后一種是最好的,知識(shí)不是一步登天,需要在問題上不斷改進(jìn),獲得最終的解決方案,當(dāng)然最后一種不一定是最好的,實(shí)現(xiàn)策略上,如果可以利用數(shù)據(jù)庫也是可以的,但我認(rèn)為每次訪問網(wǎng)站都要讀和寫數(shù)據(jù)庫,這樣效率就降低了。
- jsp利用echarts實(shí)現(xiàn)報(bào)表統(tǒng)計(jì)的實(shí)例
- JS+JSP通過img標(biāo)簽調(diào)用實(shí)現(xiàn)靜態(tài)頁面訪問次數(shù)統(tǒng)計(jì)的方法
- jsp利用application統(tǒng)計(jì)在線人數(shù)的方法
- jsp的九大內(nèi)置對(duì)象深入講解
- springboot整合jsp,實(shí)現(xiàn)公交車站路線圖
- SpringBoot+MybatisPlus+Mysql+JSP實(shí)戰(zhàn)
- 如何將JSP/Servlet項(xiàng)目轉(zhuǎn)換為Spring Boot項(xiàng)目
- idea springboot 修改css,jsp不重啟實(shí)現(xiàn)頁面更新的問題
- Springboot集成jsp及部署服務(wù)器實(shí)現(xiàn)原理
- 教你怎么用JSP統(tǒng)計(jì)網(wǎng)站訪問人數(shù)
相關(guān)文章
Java之JSP教程九大內(nèi)置對(duì)象詳解(中篇)
這篇文章主要介紹了Java之JSP教程九大內(nèi)置對(duì)象詳解(中篇),本文章內(nèi)容詳細(xì),通過案例可以更好的理解JSP內(nèi)置對(duì)象的相關(guān)知識(shí),本模塊分為了三部分,本次為中篇,講解了三個(gè)內(nèi)容,需要的朋友可以參考下2023-01-01
struts2+jsp實(shí)現(xiàn)文件上傳的方法
這篇文章主要介紹了struts2+jsp實(shí)現(xiàn)文件上傳的方法,涉及JSP基于Struts架構(gòu)實(shí)現(xiàn)文件傳輸?shù)耐暾记?具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
JSP中動(dòng)態(tài)合并單元格的實(shí)例代碼
本文通過實(shí)例代碼給大家詳細(xì)介紹了jsp動(dòng)態(tài)合并單元格的方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),需要的朋友參考下吧2016-12-12
jsp中實(shí)現(xiàn)上傳圖片即時(shí)顯示效果功能
上傳圖片時(shí)即時(shí)顯示圖片效果,這在項(xiàng)目開發(fā)中時(shí)很常見的一項(xiàng)功能,接下來介紹此功能的實(shí)現(xiàn)過程,有需要的朋友可以參考下2013-01-01
jsp 從web.xml讀取連接數(shù)據(jù)庫的參數(shù)
web.xml讀取連接數(shù)據(jù)庫的參數(shù),實(shí)現(xiàn)代碼。2009-05-05
jsp Response對(duì)象頁面重定向、時(shí)間的動(dòng)態(tài)顯示
response對(duì)象主要用于對(duì)客戶端的請(qǐng)求進(jìn)行回應(yīng),將web服務(wù)器處理后的結(jié)果發(fā)回給客戶端,封裝了jsp產(chǎn)生的響應(yīng),并發(fā)送到客戶端響應(yīng)客戶端的請(qǐng)求,請(qǐng)求的數(shù)據(jù)可以是各種數(shù)據(jù)類型,甚至是文件2021-07-07
JSP中EL表達(dá)式用法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了JSP頁面中支持使用的EL表達(dá)式用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

