這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)利用Servlet怎么實(shí)現(xiàn)一個點(diǎn)擊計(jì)數(shù)器功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
高郵網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),高郵網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為高郵數(shù)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個售后服務(wù)好的高郵做網(wǎng)站的公司定做!
一、Web頁面的點(diǎn)擊計(jì)數(shù)器
以下是基于Servlet生命周期實(shí)現(xiàn)一個簡單的頁面點(diǎn)擊計(jì)數(shù)器需要的步驟:
在這里我假設(shè)Web容器不會被重新啟動。如果Web容器被重新啟動或Servlet被銷毀,計(jì)數(shù)器將被重置。
實(shí)例:
這個例子演示了如何實(shí)現(xiàn)一個簡單的頁面點(diǎn)擊計(jì)數(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" + "<h2 align=\"center\">" + title + "</h2>\n" + "<h3 align=\"center\">" + hitCount + "</h3>\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。每次頁面刷新時,計(jì)數(shù)器的值都會加1,這將產(chǎn)生如下所示的結(jié)果:
二、網(wǎng)站點(diǎn)擊計(jì)數(shù)器
很多時候,可能有興趣知道整個網(wǎng)站的總點(diǎn)擊量。在Servlet中,這也是非常簡單的,可以使用過濾器實(shí)現(xiàn)這一點(diǎn)。
以下是實(shí)現(xiàn)一個基于過濾器生命周期的簡單的網(wǎng)站點(diǎn)擊計(jì)數(shù)器需要的步驟:
在這里我假設(shè)Web容器不會被重新啟動。如果Web容器被重新啟動或Servlet被銷毀,點(diǎn)擊計(jì)數(shù)器將被重置。
實(shí)例:
這個例子演示了如何實(shí)現(xiàn)一個簡單的網(wǎng)站點(diǎn)擊計(jì)數(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/。每次任意頁面被點(diǎn)擊時,計(jì)數(shù)器的值都會加1并且會在日志中顯示如下所示的消息:
上述就是小編為大家分享的利用Servlet怎么實(shí)現(xiàn)一個點(diǎn)擊計(jì)數(shù)器功能了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享題目:利用Servlet怎么實(shí)現(xiàn)一個點(diǎn)擊計(jì)數(shù)器功能
新聞來源:http://www.rwnh.cn/article22/pgcsjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、定制開發(fā)、網(wǎng)站制作、面包屑導(dǎo)航、全網(wǎng)營銷推廣、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)