溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用IDEA 實(shí)現(xiàn)顯示當(dāng)前在線人數(shù)和歷史訪問量功能

發(fā)布時(shí)間:2020-11-09 15:58:47 來源:億速云 閱讀:447 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)利用IDEA 實(shí)現(xiàn)顯示當(dāng)前在線人數(shù)和歷史訪問量功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

當(dāng)前在線人數(shù)

利用IDEA 實(shí)現(xiàn)顯示當(dāng)前在線人數(shù)和歷史訪問量功能

一共需要三處

創(chuàng)建監(jiān)聽器

package com.count;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/*
 初始化:
   只有服務(wù)器的啟動(dòng),才會(huì)創(chuàng)建servletContext對(duì)象。
  用于監(jiān)聽servletContext創(chuàng)建,一旦創(chuàng)建servletContext創(chuàng)建,則設(shè)置servletContext中的count值為0;
*/
@WebListener
/*
 這個(gè)注解的作用是啟動(dòng)監(jiān)聽,相當(dāng)于在web.xml配置(
 <listener>
  <listener-class>com.cyl.count.InitServletContexListener</listener-class>
 </listener>
*/
public class InitServletContexListener implements ServletContextListener {
 @Override
 public void contextInitialized(ServletContextEvent servletContextEvent) {
  //獲取ServletContext域?qū)ο?
  ServletContext servletContext = servletContextEvent.getServletContext();
  //給ServletContext域?qū)ο?,設(shè)置count=0
  servletContext.setAttribute("count",0);
 }
 
 @Override
 public void contextDestroyed(ServletContextEvent servletContextEvent) {
 
 }
}
package com.count;
 
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
 
/**
 * @監(jiān)聽在線人數(shù),監(jiān)聽session的創(chuàng)建和銷毀
 *  如果session創(chuàng)建 獲取ServletContext中的count++,重新設(shè)置
 *  如果session銷毀 獲取ServletContext中的count--,重新設(shè)置
 */
@WebListener
public class OnlineNumberHttpSessionListener implements HttpSessionListener {
 @Override
 public void sessionCreated(HttpSessionEvent httpSessionEvent) {
  //1.獲取session
  HttpSession session = httpSessionEvent.getSession();
  ServletContext servletContext = session.getServletContext();
  //2.獲取counnt值,加1
  int count = (int) servletContext.getAttribute("count");
  count++;
  //3.把servlet存儲(chǔ)到servletContext對(duì)象中
  servletContext.setAttribute("count",count);
 }
 
 @Override
 public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
 
  //1.獲取session
  HttpSession session = httpSessionEvent.getSession();
  ServletContext servletContext = session.getServletContext();
  //2.獲取counnt值,減1
  int count = (int) servletContext.getAttribute("count");
  count++;
  //3.把servlet存儲(chǔ)到servletContext對(duì)象中
  servletContext.setAttribute("count",count);
 }
}

修改index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
 <title>$Title$</title>
</head>
<body>
<h2>當(dāng)前在線人數(shù):${count}</h2>
</body>
</html>

歷史訪問量

利用IDEA 實(shí)現(xiàn)顯示當(dāng)前在線人數(shù)和歷史訪問量功能

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ù)顯示在頁面上
  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)計(jì)</title>\r\n" +
    "</head>\r\n" +
    "<body>");
  out.print("<h2>");
  out.print("您是第 "+ servletContext.getAttribute("count")+"位訪客");
  out.print("<h2>");
  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);
 }
 
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
 <head>
 <title>$Title$</title>
 </head>
 <body>
 <%
 //統(tǒng)計(jì)網(wǎng)頁訪問量
 if (application.getAttribute("count") == null) {
  application.setAttribute("count", 0);//application.setAttribute("count", new Integer(0));
 }
 Integer count = (Integer) application.getAttribute("count");
 //使用application對(duì)象讀取count參數(shù)的值,再在原值基礎(chǔ)上累加1
 application.setAttribute("count", count + 1);//application.setAttribute("count", new Integer(count.intValue() + 1));
 %>
 <h3>
 <!-- 輸出累加后的count參數(shù)對(duì)應(yīng)的值 -->
 歡迎您訪問,本頁面已經(jīng)被訪問過 <font color="#ff0000"><%=application.getAttribute("count")%></font>次
 </h3>
 </body>
</html>

看完上述內(nèi)容,你們對(duì)利用IDEA 實(shí)現(xiàn)顯示當(dāng)前在線人數(shù)和歷史訪問量功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI