基于servlet實現(xiàn)統(tǒng)計網(wǎng)頁訪問次數(shù)
本文實例為大家分享了基于servlet實現(xiàn)統(tǒng)計網(wǎng)頁訪問次數(shù)的具體代碼,供大家參考,具體內容如下
一、基礎知識
(1)ServletContext和ServletConfig的區(qū)別
ServletContext作為整個web應用的共享數(shù)據(jù)
ServletConfig只是作為當前servlet的數(shù)據(jù)共享,下一個servlet訪問時,是訪問不到的
二、代碼實現(xiàn)
將顯示的統(tǒng)計次數(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 {
?? ??? ?//設置字符編碼
?? ??? ?request.setCharacterEncoding("utf-8");
?? ??? ?response.setCharacterEncoding("utf-8");
?? ??? ?response.setContentType("text/html; charset=utf-8");
?? ??? ?
?? ??? ?//獲取全局的共享數(shù)據(jù)
?? ??? ?ServletContext servletContext = this.getServletContext();
?? ??? ?
?? ??? ?//獲取計數(shù)器count
?? ??? ?Integer count = (Integer) servletContext.getAttribute("count");
?? ??? ?
?? ??? ?//如果獲取的計算器對象為空 ,說明是第一次訪問,并將count,放入servletCount
?? ??? ?if( servletContext.getAttribute("count") == null) {
?? ??? ??? ?count = 1;
?? ??? ??? ?servletContext.setAttribute("count", count);
?? ??? ?}else {
?? ??? ??? ?//否則就不是第一次訪問,將登陸的計數(shù)器進行加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)計</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ù)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot配置Redis連接池的實現(xiàn)步驟
本文主要介紹了SpringBoot配置Redis連接池的實現(xiàn)步驟,詳細的講解了連接池的作用、配置方式、連接池參數(shù)說明,具有一定的參考價值,感興趣的可以了解一下2025-03-03
Java中PageHelper分頁后對list操作導致分頁無效
在項目中使用分頁插件的時候發(fā)現(xiàn)PageHelper插件失效了,本文就來介紹一下Java中PageHelper分頁后對list操作導致分頁無效的解決方法,感興趣的可以了解一下2021-05-05

