jsp網站訪問次數統計


JSP 點擊量統計

有時候我們需要知道某個頁面被訪問的次數,這時我們就需要在頁面上添加頁面統計器,頁面訪問的統計一般在用戶第一次載入時累加該頁面的訪問數上。

要實現一個計數器,您可以利用應用程序隱式對象和相關方法getAttribute()和setAttribute()來實現。

這個對象表示JSP頁面的整個生命周期中。當JSP頁面初始化時創建此對象,當JSP頁面調用jspDestroy()時刪除該對象。

以下是在應用中創建變量的語法:

application.setAttribute(String Key, Object Value);

您可以使用上述方法來設置一個計數器變量及更新該變量的值。讀取該變量的方法如下:

application.getAttribute(String Key);

在頁面每次被訪問時,你可以讀取計數器的當前值,並遞增1,然后重新設置,在下一個用戶訪問時就將新的值顯示在頁面上。


實例演示

該實例將介紹如何使用JSP來計算特定頁面訪問的總人數。如果你要計算你網站使用頁面的總點擊量,那么你就必須將該代碼放在所有的JSP頁面上。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*" %> <html> <html> <head> <title>訪問量統計</title> </head> <body> <% Integer hitsCount = (Integer)application.getAttribute("hitCounter"); if( hitsCount ==null || hitsCount == 0 ){ /* 第一次訪問 */ out.println("歡迎訪問菜鳥教程!"); hitsCount = 1; }else{ /* 返回訪問值 */ out.println("歡迎再次訪問菜鳥教程!"); hitsCount += 1; } application.setAttribute("hitCounter", hitsCount); %> <p>頁面訪問量為: <%= hitsCount%></p> </body> </html>

現在我們將上面的代碼放置於main.jsp文件上,並訪問http://localhost:8080/testjsp/main.jsp文件。你會看到頁面會生成個計數器,在我們每次刷新頁面時,計數器都會發生變化(每次刷新增加1)。

你也可以通過不同的瀏覽器訪問,計數器會在每次訪問后增加1。如下所示:

 


復位計數器

使用以上方法,在 web 服務器重啟后,計數器會被復位為 0,即前面保留的數據都會消失,你可以使用以下幾種方式解決該問題:

  • 在數據庫中定義一個用於統計網頁訪問量的數據表 count,字段為 hitcount,hitcount 默認值為0,將統計數據寫入到數據表中。
  • 在每次訪問時我們讀取表中 hitcount 字段。

     

  • 每次訪問時讓 hitcount 自增 1。
  • 在頁面上顯示新的 hitcount 值作為頁面的訪問量。

     

  • 如果你需要統計每個頁面的訪問量,你可以使用以上邏輯將代碼添加到所有頁面上。

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

方式二.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>我們的友誼海枯石爛! 你是第&nbsp;<%=count %>&nbsp;位訪客</p>
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM