我采用的是jsp網頁,但是不管采用什么語言,原理是一樣的。
第一種,單頁面統計。就是說,只要點擊這個頁面就會統計一次。
<body> <%!//在這種標記中定義的變量為全局變量
int count=0;
synchronized void count(){
count++;
}
%> <% count(); out.println("這是第"+count+"個訪問者!"); %> </body>
第二種,是利用jsp的內置對象application進行統計。這個程序結果運行分析,也是訪問一次頁面統計一次。感覺還是不夠好。真正滿意的是瀏覽器打開網頁,到關閉網頁算一次,這樣統計比較實際。
<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>
第三種,利用jsp的application和session進行統計。它的原理是,訪問者打開瀏覽器到關閉瀏覽器算一次訪問。每次打開首頁,創建一個session,這個session直到瀏覽器關閉才失效。但總體來說,比前兩種要好。但是有一個一個缺陷,那就是當jsp服務器重啟時,累計的統計數就清零了。
<% int n = 0; String count = (String) application.getAttribute("counter"); if (counter != null) n = Integer.parseInt(counter); if (session.isNew())
++n; out.print("你是第" + n + "位訪客"); counter = String.valueOf(n); application.setAttribute("counter", counter); %>
第四種,就是保存到txt文本中,那樣重啟服務器也不會丟失了。
public class counter { public static void writeFile(String filename, long count) { try { PrintWriter out = newPrintWriter(new FileWriter(filename)); out.println(count); out.close(); } catch (IOException e) { e.printStackTrace(); } } public static long readFile(String filename) { File f = new File(filename); long count = 0; if (!f.exists()) { writeFile(filename, 0); } try { BufferedReader in = newBufferedReader(newFileReader(f)); try { count = Long.parseLong(in.readLine()); } catch (NumberFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } return count; } }
下面是你要計數的jsp頁面,在里面添上以下內容就ok了
<%@pageimport="com.benb.servlet.counter"%> <% counterCountFileHandler=newcounter();//創建對象 longcount=CountFileHandler.readFile(request.getRealPath("/")+"count.txt"); //讀取文件獲取數據賦給count count=count+1;//修改記錄,數據加1 out.println(count);//顯示記錄數 CountFileHandler.writeFile(request.getRealPath("/")+"count.txt",count);//更新文件記錄 %>
但是還是不是很好,也是每次訪問首頁就計數一次。怎么樣百分百滿意呢?
最后一種完美解決方法,session和application加文本保存結合就完美了,不管重啟服務器,還是能百分百記錄所有的訪問記錄。
寫一個severlet類似前面,就是long類型改成int類型。
public class Counter extends HttpServlet { private static final long serialVersionUID = 1L; public Counter() { super(); } public static void writeFile(String filename, int count) { try { PrintWriter out = new PrintWriter(new FileWriter(filename)); out.println(count); out.close(); } catch (IOException e) { e.printStackTrace(); } } public static int readFile(String filename) { File f = new File(filename); int count = 0; if (!f.exists()) { writeFile(filename, 0); } try { BufferedReader in = new BufferedReader(new FileReader(f)); try { count = Integer.parseInt(in.readLine()); } catch (NumberFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } return count; } public void init() throws ServletException { // Put your code here } }
頁面編碼如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="com.tozhan.cn.GetDataDB" %> <%@ page import="com.tozhan.cn.news.New" %> <%@ page import="com.tozhan.cn.Counter" %> <% Counter CountFileHandler=new Counter();//創建對象 int count=0; if(application.getAttribute("count")==null){ count=CountFileHandler.readFile(request.getRealPath("/")+"count.txt"); //讀取文件獲取數據賦給count application.setAttribute("count",new Integer(count)); } count=(Integer)application.getAttribute("count"); if(session.isNew()) ++count; application.setAttribute("count",count); CountFileHandler.writeFile(request.getRealPath("/")+"count.txt",count);//更新文件記錄 %> <p>我們的友誼海枯石爛! 你是第 <%=count %> 位訪客</p>