溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用servlet實(shí)現(xiàn)統(tǒng)計(jì)網(wǎng)頁訪問次數(shù)

發(fā)布時間:2022-02-09 11:35:07 來源:億速云 閱讀:300 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹如何使用servlet實(shí)現(xiàn)統(tǒng)計(jì)網(wǎng)頁訪問次數(shù),文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

一、基礎(chǔ)知識

(1)ServletContext和ServletConfig的區(qū)別

ServletContext作為整個web應(yīng)用的共享數(shù)據(jù)
ServletConfig只是作為當(dāng)前servlet的數(shù)據(jù)共享,下一個servlet訪問時,是訪問不到的

二、代碼實(shí)現(xiàn)

將顯示的統(tǒng)計(jì)次數(shù)顯示在HTML頁面上:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
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 countServlet1
 */
@WebServlet("/countServlet1")
public class countServlet1 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public countServlet1() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //設(shè)置字符編碼
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        
        //獲取全局的共享數(shù)據(jù)
        ServletContext servletContext = this.getServletContext();
        
        //獲取計(jì)數(shù)器count
        Integer count = (Integer) servletContext.getAttribute("count");
        
        //如果獲取的計(jì)算器對象為空 ,說明是第一次訪問,并將count,放入servletCount
        if( servletContext.getAttribute("count") == null) {
            count = 1;
            servletContext.setAttribute("count", count);
        }else {
            //否則就不是第一次訪問,將登陸的計(jì)數(shù)器進(jìn)行加1的數(shù)據(jù)更新
            servletContext.setAttribute("count", count+1);
        }
        
        //將登陸的次數(shù)顯示在頁面上
        PrintWriter out =response.getWriter();
        out.print("<!DOCTYPE html>\r\n" + 
                  "<html>\r\n" + 
                  "<head>\r\n" + 
                  "<meta charset=\"UTF-8\">\r\n" + 
                  "<title>登陸網(wǎng)頁次數(shù)統(tǒng)計(jì)</title>\r\n" + 
                  "</head>\r\n" + 
                  "<body>");
        out.print("<h2>");
        out.print("您是第 "+ servletContext.getAttribute("count")+"位訪客");
        out.print("<h2>");
        out.print("</body>\r\n" + 
                  "</html>

}

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

三、在不同瀏覽器顯示的次數(shù)

(1)在eclipse中顯示的次數(shù)

如何使用servlet實(shí)現(xiàn)統(tǒng)計(jì)網(wǎng)頁訪問次數(shù)

(2)在火狐中顯示的次數(shù)

如何使用servlet實(shí)現(xiàn)統(tǒng)計(jì)網(wǎng)頁訪問次數(shù)

(3)在360中顯示的次數(shù)

如何使用servlet實(shí)現(xiàn)統(tǒng)計(jì)網(wǎng)頁訪問次數(shù)

以上是“如何使用servlet實(shí)現(xiàn)統(tǒng)計(jì)網(wǎng)頁訪問次數(shù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI