基于servlet實現統(tǒng)計網頁訪問次數
本文實例為大家分享了基于servlet實現統(tǒng)計網頁訪問次數的具體代碼,供大家參考,具體內容如下
一、基礎知識
(1)ServletContext和ServletConfig的區(qū)別
ServletContext作為整個web應用的共享數據
ServletConfig只是作為當前servlet的數據共享,下一個servlet訪問時,是訪問不到的
二、代碼實現
將顯示的統(tǒng)計次數顯示在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 { ?? ??? ?//設置字符編碼 ?? ??? ?request.setCharacterEncoding("utf-8"); ?? ??? ?response.setCharacterEncoding("utf-8"); ?? ??? ?response.setContentType("text/html; charset=utf-8"); ?? ??? ? ?? ??? ?//獲取全局的共享數據 ?? ??? ?ServletContext servletContext = this.getServletContext(); ?? ??? ? ?? ??? ?//獲取計數器count ?? ??? ?Integer count = (Integer) servletContext.getAttribute("count"); ?? ??? ? ?? ??? ?//如果獲取的計算器對象為空 ,說明是第一次訪問,并將count,放入servletCount ?? ??? ?if( servletContext.getAttribute("count") == null) { ?? ??? ??? ?count = 1; ?? ??? ??? ?servletContext.setAttribute("count", count); ?? ??? ?}else { ?? ??? ??? ?//否則就不是第一次訪問,將登陸的計數器進行加1的數據更新 ?? ??? ??? ?servletContext.setAttribute("count", count+1); ?? ??? ?} ?? ??? ? ?? ??? ?//將登陸的次數顯示在頁面上 ?? ??? ?PrintWriter out =response.getWriter(); ?? ??? ?out.print("<!DOCTYPE html>\r\n" +? ?? ??? ??? ??? ? ?"<html>\r\n" +? ?? ??? ??? ??? ? ?"<head>\r\n" +? ?? ??? ??? ??? ? ?"<meta charset=\"UTF-8\">\r\n" +? ?? ??? ??? ??? ? ?"<title>登陸網頁次數統(tǒng)計</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); ?? ?} }
三、在不同瀏覽器顯示的次數
(1)在eclipse中顯示的次數
(2)在火狐中顯示的次數
(3)在360中顯示的次數
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot整合Mybatis-plus案例及用法實例
mybatis-plus是一個 Mybatis 的增強工具,在 Mybatis 的基礎上只做增強不做改變,為簡化開發(fā)、提高效率而生,下面這篇文章主要給大家介紹了關于SpringBoot整合Mybatis-plus案例及用法實例的相關資料,需要的朋友可以參考下2022-11-11基于Tomcat7、Java、WebSocket的服務器推送聊天室實例
HTML5 WebSocket實現了服務器與瀏覽器的雙向通訊,本篇文章主要介紹了基于Tomcat7、Java、WebSocket的服務器推送聊天室實例,具有一定的參考價值,有興趣的可以了解一下。2016-12-12自從在 IDEA 中用了熱部署神器 JRebel 之后,開發(fā)效率提升了 10(真棒)
在javaweb開發(fā)過程中,使用熱部署神器 JRebel可以使class類還是更新spring配置文件都能立馬見到效率,本文給大家介紹JRebel的兩種安裝方法,小編建議使用第二種方法,具體安裝步驟跟隨小編一起看看吧2021-06-06Java實戰(zhàn)之電影在線觀看系統(tǒng)的實現
這篇文章主要介紹了如何利用Java實現電影在線觀看系統(tǒng),文中用到的技術有:JSP、Spring、SpringMVC、MyBatis等,感興趣的可以了解一下2022-04-04