基于servlet實現統(tǒng)計網頁訪問次數
更新時間:2022年02月09日 10:57:42 作者:一只小小的螞蟻
這篇文章主要為大家詳細介紹了基于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中顯示的次數

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
Java中PageHelper分頁后對list操作導致分頁無效
在項目中使用分頁插件的時候發(fā)現PageHelper插件失效了,本文就來介紹一下Java中PageHelper分頁后對list操作導致分頁無效的解決方法,感興趣的可以了解一下2021-05-05

