基于servlet實(shí)現(xiàn)統(tǒng)計(jì)網(wǎng)頁(yè)訪問次數(shù)
本文實(shí)例為大家分享了基于servlet實(shí)現(xiàn)統(tǒng)計(jì)網(wǎng)頁(yè)訪問次數(shù)的具體代碼,供大家參考,具體內(nèi)容如下
一、基礎(chǔ)知識(shí)
(1)ServletContext和ServletConfig的區(qū)別
ServletContext作為整個(gè)web應(yīng)用的共享數(shù)據(jù)
ServletConfig只是作為當(dāng)前servlet的數(shù)據(jù)共享,下一個(gè)servlet訪問時(shí),是訪問不到的
二、代碼實(shí)現(xiàn)
將顯示的統(tǒng)計(jì)次數(shù)顯示在HTML頁(yè)面上:
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ì)算器對(duì)象為空 ,說明是第一次訪問,并將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ù)顯示在頁(yè)面上
?? ??? ?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)頁(yè)次數(shù)統(tǒng)計(jì)</title>\r\n" +?
?? ??? ??? ??? ? ?"</head>\r\n" +?
?? ??? ??? ??? ? ?"<body>");
?? ??? ?out.print("<h1>");
?? ??? ?out.print("您是第 "+ servletContext.getAttribute("count")+"位訪客");
?? ??? ?out.print("<h1>");
?? ??? ?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ù)

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

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot配置Redis連接池的實(shí)現(xiàn)步驟
本文主要介紹了SpringBoot配置Redis連接池的實(shí)現(xiàn)步驟,詳細(xì)的講解了連接池的作用、配置方式、連接池參數(shù)說明,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03
servlet上傳文件實(shí)現(xiàn)代碼詳解(四)
這篇文章主要為大家詳細(xì)介紹了servlet上傳文件的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Java中PageHelper分頁(yè)后對(duì)list操作導(dǎo)致分頁(yè)無(wú)效
在項(xiàng)目中使用分頁(yè)插件的時(shí)候發(fā)現(xiàn)PageHelper插件失效了,本文就來介紹一下Java中PageHelper分頁(yè)后對(duì)list操作導(dǎo)致分頁(yè)無(wú)效的解決方法,感興趣的可以了解一下2021-05-05
java swing實(shí)現(xiàn)貪吃蛇雙人游戲
這篇文章主要為大家詳細(xì)介紹了java swing實(shí)現(xiàn)貪吃蛇雙人小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
Jmeter后置處理器實(shí)現(xiàn)過程及方法應(yīng)用
這篇文章主要介紹了Jmeter后置處理器實(shí)現(xiàn)過程及方法應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
關(guān)于Java?CPU或內(nèi)存使用率過高問題定位
Spring?cloud微服務(wù)廣泛應(yīng)用后,服務(wù)的監(jiān)控和運(yùn)維壓力也與日俱增,經(jīng)常有服務(wù)出現(xiàn)CPU或者內(nèi)存使用率過高的告警,那么遇到這樣的問題我們?cè)撊绾闻挪槟??我們可以借助哪些工具來定位問題呢?本文將介紹一下遇到此類問題的解決思路和方法2024-10-10

