基于servlet實(shí)現(xiàn)統(tǒng)計(jì)網(wǎng)頁(yè)訪問(wèn)次數(shù)
本文實(shí)例為大家分享了基于servlet實(shí)現(xiàn)統(tǒng)計(jì)網(wǎng)頁(yè)訪問(wèn)次數(shù)的具體代碼,供大家參考,具體內(nèi)容如下
一、基礎(chǔ)知識(shí)
(1)ServletContext和ServletConfig的區(qū)別
ServletContext作為整個(gè)web應(yīng)用的共享數(shù)據(jù)
ServletConfig只是作為當(dāng)前servlet的數(shù)據(jù)共享,下一個(gè)servlet訪問(wèn)時(shí),是訪問(wèn)不到的
二、代碼實(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ì)象為空 ,說(shuō)明是第一次訪問(wèn),并將count,放入servletCount ?? ??? ?if( servletContext.getAttribute("count") == null) { ?? ??? ??? ?count = 1; ?? ??? ??? ?servletContext.setAttribute("count", count); ?? ??? ?}else { ?? ??? ??? ?//否則就不是第一次訪問(wèn),將登陸的計(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)文章
J2SE基礎(chǔ)之在Eclipse中運(yùn)行hello world
本文的內(nèi)容非常的簡(jiǎn)單,跟隨世界潮流,第一個(gè)Java程序輸出“Hell World!”。希望大家能夠喜歡2016-05-05SpringBoot整合Mybatis-plus案例及用法實(shí)例
mybatis-plus是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生,下面這篇文章主要給大家介紹了關(guān)于SpringBoot整合Mybatis-plus案例及用法實(shí)例的相關(guān)資料,需要的朋友可以參考下2022-11-11Java+Swing實(shí)現(xiàn)中國(guó)象棋游戲
這篇文章將通過(guò)Java+Swing實(shí)現(xiàn)經(jīng)典的中國(guó)象棋游戲。文中可以實(shí)現(xiàn)開(kāi)始游戲,悔棋,退出等功能。感興趣的小伙伴可以跟隨小編一起動(dòng)手試一試2022-02-02Java分批將List數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)的解決過(guò)程
這篇文章主要給大家介紹了關(guān)于Java分批將List數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)的解決過(guò)程,文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-08-08java實(shí)現(xiàn)簡(jiǎn)單TCP聊天程序
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單TCP聊天程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07基于Tomcat7、Java、WebSocket的服務(wù)器推送聊天室實(shí)例
HTML5 WebSocket實(shí)現(xiàn)了服務(wù)器與瀏覽器的雙向通訊,本篇文章主要介紹了基于Tomcat7、Java、WebSocket的服務(wù)器推送聊天室實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12自從在 IDEA 中用了熱部署神器 JRebel 之后,開(kāi)發(fā)效率提升了 10(真棒)
在javaweb開(kāi)發(fā)過(guò)程中,使用熱部署神器 JRebel可以使class類(lèi)還是更新spring配置文件都能立馬見(jiàn)到效率,本文給大家介紹JRebel的兩種安裝方法,小編建議使用第二種方法,具體安裝步驟跟隨小編一起看看吧2021-06-06Java實(shí)戰(zhàn)之電影在線觀看系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Java實(shí)現(xiàn)電影在線觀看系統(tǒng),文中用到的技術(shù)有:JSP、Spring、SpringMVC、MyBatis等,感興趣的可以了解一下2022-04-04