Servlet實現點擊計數器
以下內容引用自http://wiki.jikexueyuan.com/project/servlet/hits-counter.html:
一、Web頁面的點擊計數器
很多時候,可能有興趣知道網站的某個特定頁面上的總點擊量。使用Servlet來計算這些點擊量是非常簡單的,因為一個Servlet的生命周期是由它運行的容器控制的。
以下是基於Servlet生命周期實現一個簡單的頁面點擊計數器需要的步驟:
-
在init()方法中初始化一個全局變量。
-
每次調用doGet()或doPost()方法時,增加全局變量。
-
如果需要,可以使用一個數據庫表來存儲destroy()方法中的全局變量。在下次初始化Servlet時,這個值可以在init()方法內被讀取。這一步是可選的。
-
如果想計算一個會話內一個頁面的點量擊,那麽可以使用isNew()方法來查看該會話內是否已點擊過相同的頁面。這一步是可選的。
- 可以顯示全局計數器的值來顯示網站中的總點擊量。這一步是可選的。
在這裏我假設Web容器不會被重新啟動。如果Web容器被重新啟動或Servlet被銷毀,計數器將被重置。
實例:
這個例子演示了如何實現一個簡單的頁面點擊計數器:
import java.io.*; import java.sql.Date; import java.util.*; import javax.servlet.*; import javax.servlet.http.*;public class PageHitCounter extends HttpServlet{ private int hitCount; public void init() { // Reset hit counter. hitCount = 0; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// Set response content type response.setContentType("text/html"); // This method executes whenever the servlet is hit // increment hitCount hitCount++; PrintWriter out = response.getWriter(); String title = "Total Number of Hits"; String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\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() { // This is optional step but if you like you // can write hitCount value in your database. } }
現在編譯上述Servlet並在web.xml文件中創建以下條目:
.... <servlet> <servlet-name>PageHitCounter</servlet-name> <servlet-class>PageHitCounter</servlet-class> </servlet> <servlet-mapping> <servlet-name>PageHitCounter</servlet-name> <url-pattern>/PageHitCounter</url-pattern> </servlet-mapping> ....
現在使用URL http://localhost:8080/PageHitCounter
來調用這個Servlet。每次頁面刷新時,計數器的值都會加1,這將產生如下所示的結果:
二、網站點擊計數器
很多時候,可能有興趣知道整個網站的總點擊量。在Servlet中,這也是非常簡單的,可以使用過濾器實現這一點。
以下是實現一個基於過濾器生命周期的簡單的網站點擊計數器需要的步驟:
-
在過濾器的init()方法中初始化一個全局變量。
-
每次調用doFilter方法時,增加全局變量。
- 如果需要,可以使用一個數據庫表來存儲過濾器的destroy()方法中的全局變量的值。在下次初始化過濾器時,該值可以在init()方法內被讀取。這一步是可選的。
在這裏我假設Web容器不會被重新啟動。如果Web容器被重新啟動或Servlet被銷毀,點擊計數器將被重置。
實例:
這個例子演示了如何實現一個簡單的網站點擊計數器:
// Import required java libraries 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{ // Reset hit counter. hitCount = 0; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException { // increase counter by one hitCount++; // Print the counter. System.out.println("Site visits count :"+ hitCount ); // Pass request back down the filter chain chain.doFilter(request,response); } public void destroy() { // This is optional step but if you like you // can write hitCount value in your database. } }
現在來編譯上述Servlet並在web.xml文件中創建以下條目:
.... <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> ....
現在調用任意URL如URL:http://localhost:8080/
。每次任意頁面被點擊時,計數器的值都會加1並且會在日誌中顯示如下所示的消息:
測試工程:https://github.com/easonjim/5_java_example/tree/master/servletbasics/test14
Servlet實現點擊計數器