很多時候,您可能有興趣知道網(wǎng)站的某個特定頁面上的總點擊量。使用 Servlet 來計算這些點擊量是非常簡單的,因為一個 Servlet 的生命周期是由它運行所在的容器控制的。
以下是實現(xiàn)一個簡單的基于 Servlet 生命周期的網(wǎng)頁點擊計數(shù)器需要采取的步驟:
在 init() 方法中初始化一個全局變量。
每次調(diào)用 doGet() 或 doPost() 方法時,都增加全局變量。
如果需要,您可以使用一個數(shù)據(jù)庫表來存儲全局變量的值在 destroy() 中。在下次初始化 Servlet 時,該值可在 init() 方法內(nèi)被讀取。這一步是可選的。
如果您只想對一個 session 會話計數(shù)一次頁面點擊,那么請使用 isNew() 方法來檢查該 session 會話是否已點擊過相同頁面。這一步是可選的。
您可以通過顯示全局計數(shù)器的值,來在網(wǎng)站上展示頁面的總點擊量。這一步是可選的。
在這里,我們假設(shè) Web 容器將無法重新啟動。如果是重新啟動或 Servlet 被銷毀,計數(shù)器將被重置。
本示例演示了如何實現(xiàn)一個簡單的網(wǎng)頁點擊計數(shù)器:
package com.nhooo.test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class PageHitCounter
*/
@WebServlet("/PageHitCounter")
public class PageHitCounter extends HttpServlet {
private static final long serialVersionUID = 1L;
private int hitCount;
public void init()
{
// 重置點擊計數(shù)器
hitCount = 0;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
// 增加 hitCount
hitCount++;
PrintWriter out = response.getWriter();
String title = "總點擊量";
String docType = "<!DOCTYPE html> \n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<h2 align=\"center\">" + hitCount + "</h2>\n" +
"</body></html>");
}
public void destroy()
{
// 這一步是可選的,但是如果需要,您可以把 hitCount 的值寫入到數(shù)據(jù)庫
}
}現(xiàn)在讓我們來編譯上面的 Servlet,并在 web.xml 文件中創(chuàng)建以下條目:
<?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>PageHitCounter</servlet-name> <servlet-class>com.nhooo.test.PageHitCounter</servlet-class> </servlet> <servlet-mapping> <servlet-name>PageHitCounter</servlet-name> <url-pattern>/TomcatTest/PageHitCounter</url-pattern> </servlet-mapping> </web-app>
現(xiàn)在通過訪問 http://localhost:8080/TomcatTest/PageHitCounter 來調(diào)用這個 Servlet。這將會在每次頁面刷新時,把計數(shù)器的值增加 1,結(jié)果如下所示:
總點擊量6 |
很多時候,您可能有興趣知道整個網(wǎng)站的總點擊量。在 Servlet 中,這也是非常簡單的,我們可以使用過濾器做到這一點。
以下是實現(xiàn)一個簡單的基于過濾器生命周期的網(wǎng)站點擊計數(shù)器需要采取的步驟:
在過濾器的 init() 方法中初始化一個全局變量。
每次調(diào)用 doFilter 方法時,都增加全局變量。
如果需要,您可以在過濾器的 destroy() 中使用一個數(shù)據(jù)庫表來存儲全局變量的值。在下次初始化過濾器時,該值可在 init() 方法內(nèi)被讀取, 這一步是可選的。
在這里,我們假設(shè) Web 容器將無法重新啟動。如果是重新啟動或 Servlet 被銷毀,點擊計數(shù)器將被重置。
本示例演示了如何實現(xiàn)一個簡單的網(wǎng)站點擊計數(shù)器:
// 導(dǎo)入必需的 java 庫
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class SiteHitCounter implements Filter{
private int hitCount;
public void init(FilterConfig config)
throws ServletException{
// 重置點擊計數(shù)器
hitCount = 0;
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws java.io.IOException, ServletException {
// 把計數(shù)器的值增加 1
hitCount++;
// 輸出計數(shù)器
System.out.println("網(wǎng)站訪問統(tǒng)計:"+ hitCount );
// 把請求傳回到過濾器鏈
chain.doFilter(request,response);
}
public void destroy()
{
// 這一步是可選的,但是如果需要,您可以把 hitCount 的值寫入到數(shù)據(jù)庫
}
}現(xiàn)在讓我們來編譯上面的 Servlet,并在 web.xml 文件中創(chuàng)建以下條目:
.... <filter> <filter-name>SiteHitCounter</filter-name> <filter-class>SiteHitCounter</filter-class> </filter> <filter-mapping> <filter-name>SiteHitCounter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ....
現(xiàn)在訪問網(wǎng)站的任意頁面,比如 http://localhost:8080/。這將會在每次任意頁面被點擊時,把計數(shù)器的值增加 1,它會在日志中顯示以下消息:
網(wǎng)站訪問統(tǒng)計: 1 網(wǎng)站訪問統(tǒng)計: 2 網(wǎng)站訪問統(tǒng)計: 3 網(wǎng)站訪問統(tǒng)計: 4 網(wǎng)站訪問統(tǒng)計: 5 ..................