Servlet實現(xiàn)點擊計數(shù)器的方法
一、Web頁面的點擊計數(shù)器
很多時候,可能有興趣知道網(wǎng)站的某個特定頁面上的總點擊量。使用Servlet來計算這些點擊量是非常簡單的,因為一個Servlet的生命周期是由它運行的容器控制的。
以下是基于Servlet生命周期實現(xiàn)一個簡單的頁面點擊計數(shù)器需要的步驟:
- 在init()方法中初始化一個全局變量。
- 每次調(diào)用doGet()或doPost()方法時,增加全局變量。
- 如果需要,可以使用一個數(shù)據(jù)庫表來存儲destroy()方法中的全局變量。在下次初始化Servlet時,這個值可以在init()方法內(nèi)被讀取。這一步是可選的。
- 如果想計算一個會話內(nèi)一個頁面的點量擊,那么可以使用isNew()方法來查看該會話內(nèi)是否已點擊過相同的頁面。這一步是可選的。
- 可以顯示全局計數(shù)器的值來顯示網(wǎng)站中的總點擊量。這一步是可選的。
在這里我假設(shè)Web容器不會被重新啟動。如果Web容器被重新啟動或Servlet被銷毀,計數(shù)器將被重置。
實例:
這個例子演示了如何實現(xiàn)一個簡單的頁面點擊計數(shù)器:
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.
}
}
現(xiàn)在編譯上述Servlet并在web.xml文件中創(chuàng)建以下條目:
.... <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> ....
現(xiàn)在使用URL http://localhost:8080/PageHitCounter來調(diào)用這個Servlet。每次頁面刷新時,計數(shù)器的值都會加1,這將產(chǎn)生如下所示的結(jié)果:

二、網(wǎng)站點擊計數(shù)器
很多時候,可能有興趣知道整個網(wǎng)站的總點擊量。在Servlet中,這也是非常簡單的,可以使用過濾器實現(xiàn)這一點。
以下是實現(xiàn)一個基于過濾器生命周期的簡單的網(wǎng)站點擊計數(shù)器需要的步驟:
- 在過濾器的init()方法中初始化一個全局變量。
- 每次調(diào)用doFilter方法時,增加全局變量。
- 如果需要,可以使用一個數(shù)據(jù)庫表來存儲過濾器的destroy()方法中的全局變量的值。在下次初始化過濾器時,該值可以在init()方法內(nèi)被讀取。這一步是可選的。
在這里我假設(shè)Web容器不會被重新啟動。如果Web容器被重新啟動或Servlet被銷毀,點擊計數(shù)器將被重置。
實例:
這個例子演示了如何實現(xiàn)一個簡單的網(wǎng)站點擊計數(shù)器:
// 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.
}
}
現(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)在調(diào)用任意URL如URL:http://localhost:8080/。每次任意頁面被點擊時,計數(shù)器的值都會加1并且會在日志中顯示如下所示的消息:

測試工程:https://github.com/easonjim/5_java_example/tree/master/servletbasics/test14
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合ElasticSearch的示例代碼
本篇文章主要介紹了SpringBoot整合ElasticSearch的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
Java并發(fā)工具類之CountDownLatch詳解
這篇文章主要介紹了Java并發(fā)工具類之CountDownLatch詳解,CountDownLatch可以使一個獲多個線程等待其他線程各自執(zhí)行完畢后再執(zhí)行,CountDownLatch可以解決那些一個或者多個線程在執(zhí)行之前必須依賴于某些必要的前提業(yè)務(wù)先執(zhí)行的場景,需要的朋友可以參考下2023-12-12

