JSP中實現網頁訪問統計的方法
最近學習Jave EE 中的jsp網頁開發,需要實現網頁訪問量的統計,剛開始不知道如何實現,後來問了一下老師,老師是這樣回答我的:要實現網頁訪問的統計,你可以利用application對象來實現,不能用seesion對象,因為session是屬於同一個會話的,關掉浏覽器數據就沒有了,而application是在同一浏覽器下的,只要是同一個浏覽器,將數據保存在applicaiton對象中,這樣就可以保證數據的不變性。其實這些我都懂,我只是不知道如何在jsp用代碼實現。後來我只能上網看看有沒有具體的解決方案,搜了很久,沒有我想要的答案,我想要實現的只是簡單的統計,沒有實現更加復雜的功能。後來還是在CSDN這裡找到了答案,在這裡簡單總結一下實現網頁訪問統計的幾種方法:www.2cto.com
1. 利用application對象進行統計,得到的效果是每進入一次該網頁就統計一次。但效果不怎麼好,因為一般統計網頁訪問量,刷新是不算進統計裡的,這裡就是這種缺點。
具體實現是:
[html]
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<head>
<title>java 計數器程序</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()%>個訪問者</center>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<head>
<title>java 計數器程序</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()%>個訪問者</center>
</body>
</html>
2.為了解決上面的問題,有了另一種方法,就是同時利用application對象和session對象來統計,這種方法的原理是從打開浏覽器到關閉浏覽器算是訪問一次,刷新、返回等操作不算做一次訪問。但還是有缺陷,當jsp服務器從新啟動時,數據也被清零了。
下面還是具體實現:
[html]
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<head>
<title>java 計數器程序</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);%>個訪問者</center>
<%
counter = String.valueOf(n);
application.setAttribute("counter", counter);
%>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<head>
<title>java 計數器程序</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);%>個訪問者</center>
<%
counter = String.valueOf(n);
application.setAttribute("counter", counter);
%>
</body>
</html>
3. 第三種方法是將統計數據存儲在本地的文件當中,比如存儲在一個txt文件當中。
這是為了解決重啟服務器之後數據不用擔心會丟失。
創建一個類:JSPCount
[java]
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;
}
}
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
[plain]
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page import="org.wwj.count.JSPCount" %>
<html>
<head>
<title>java 計數器程序</title>
</head>
<body>
<%
JSPCount CountFileHandler = new JSPCount();
//讀取文件
long count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");
count = count + 1; //修改記錄 +1
out.print(count); //顯示數據
//更新文件內容。
CountFileHandler.write2File(request.getRealPath("/") + "count.txt", count);
%>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page import="org.wwj.count.JSPCount" %>
<html>
<head>
<title>java 計數器程序</title>
</head>
<body>
<%
JSPCount CountFileHandler = new JSPCount();
//讀取文件
long count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt");
count = count + 1; //修改記錄 +1
out.print(count); //顯示數據
//更新文件內容。
CountFileHandler.write2File(request.getRealPath("/") + "count.txt", count);
%>
</body>
</html>
程序運行之後會在tomcat下的webapps目錄下的對應的web項目生成一個count.txt文本文件
4.第三種方法,只是保存了訪問的統計數據罷了,但沒有保證刷新頁面的時候不會自增,這樣還是不好。當然總會有解決的辦法的,一般的解決方案就是結合各種方案的優點。下面是由session對象+application對象+txt文本來實現網站的訪問統計。
[java]
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;
}
}
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文件代碼:
[plain]
<%@page import="org.servlet.count.Counter"%>
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<head>
<title>java 計數器程序</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);
}
%>
訪問人數:<%=count %>
</body>
</html>
<%@page import="org.servlet.count.Counter"%>
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
<head>
<title>java 計數器程序</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);
}
%>
訪問人數:<%=count %>
</body>
</html>