欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IDEA 當(dāng)前在線人數(shù)和歷史訪問量的示例代碼

 更新時間:2020年08月07日 14:18:51   作者:柴大大  
這篇文章主要介紹了IDEA 當(dāng)前在線人數(shù)和歷史訪問量的實例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

當(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ù)器的啟動,才會創(chuàng)建servletContext對象。
  用于監(jiān)聽servletContext創(chuàng)建,一旦創(chuàng)建servletContext創(chuàng)建,則設(shè)置servletContext中的count值為0;
*/
@WebListener
/*
 這個注解的作用是啟動監(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存儲到servletContext對象中
  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存儲到servletContext對象中
  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>
<h1>當(dāng)前在線人數(shù):${count}</h1>
</body>
</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 {
  //設(shè)置字符編碼
  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ù)器進(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)計</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);
 }
 
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
 <head>
 <title>$Title$</title>
 </head>
 <body>
 <%
 //統(tǒng)計網(wǎng)頁訪問量
 if (application.getAttribute("count") == null) {
  application.setAttribute("count", 0);//application.setAttribute("count", new Integer(0));
 }
 Integer count = (Integer) application.getAttribute("count");
 //使用application對象讀取count參數(shù)的值,再在原值基礎(chǔ)上累加1
 application.setAttribute("count", count + 1);//application.setAttribute("count", new Integer(count.intValue() + 1));
 %>
 <h2>
 <!-- 輸出累加后的count參數(shù)對應(yīng)的值 -->
 歡迎您訪問,本頁面已經(jīng)被訪問過 <font color="#ff0000"><%=application.getAttribute("count")%></font>次
 </h2>
 </body>
</html>

總結(jié)

到此這篇關(guān)于IDEA :當(dāng)前在線人數(shù)和歷史訪問量的文章就介紹到這了,更多相關(guān)IDEA :當(dāng)前在線人數(shù)和歷史訪問量內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Netty + ZooKeeper 實現(xiàn)簡單的服務(wù)注冊與發(fā)現(xiàn)

    Netty + ZooKeeper 實現(xiàn)簡單的服務(wù)注冊與發(fā)現(xiàn)

    服務(wù)注冊和發(fā)現(xiàn)一直是分布式的核心組件。本文介紹了借助 ZooKeeper 做注冊中心,如何實現(xiàn)一個簡單的服務(wù)注冊和發(fā)現(xiàn)。,需要的朋友可以參考下
    2019-06-06
  • SpringBoot配置文件application.properties的使用

    SpringBoot配置文件application.properties的使用

    這篇文章主要介紹了SpringBoot配置文件application.properties的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java面向?qū)ο蟪绦蛟O(shè)計:繼承,多態(tài)用法實例分析

    Java面向?qū)ο蟪绦蛟O(shè)計:繼承,多態(tài)用法實例分析

    這篇文章主要介紹了Java面向?qū)ο蟪绦蛟O(shè)計:繼承,多態(tài)用法,結(jié)合實例形式分析了java繼承與多態(tài)的相關(guān)概念、原理、實現(xiàn)方法與操作注意事項,需要的朋友可以參考下
    2020-04-04
  • java獲取中文拼音首字母工具類定義與用法實例

    java獲取中文拼音首字母工具類定義與用法實例

    這篇文章主要介紹了java獲取中文拼音首字母工具類定義與用法,結(jié)合實例形式分析了java獲取中文拼音首字母工具類的具體定義、使用方法及相關(guān)操作注意事項,需要的朋友可以參考下
    2019-10-10
  • Java中實現(xiàn)接口與繼承的區(qū)別及說明

    Java中實現(xiàn)接口與繼承的區(qū)別及說明

    這篇文章主要介紹了Java中實現(xiàn)接口與繼承的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 深入Java萬物之母Object類詳情

    深入Java萬物之母Object類詳情

    這篇文章主要介紹了Java萬物之母Object類詳情,Object類,它是所有類的默認(rèn)父類 ,子類不用使用extends關(guān)鍵字繼承它,不管是JDK中的類,還是自定義的類
    2022-06-06
  • java實現(xiàn)基于Tcp的socket聊天程序

    java實現(xiàn)基于Tcp的socket聊天程序

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)基于Tcp的socket聊天程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • SpringBoot?@Value與@ConfigurationProperties二者有哪些區(qū)別

    SpringBoot?@Value與@ConfigurationProperties二者有哪些區(qū)別

    這篇文章主要介紹了SpringBoot?@Value與@ConfigurationProperties二者的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-10-10
  • Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL)

    Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL)

    MyBatis-Plus是MyBatis的增強(qiáng)工具,本文主要介紹了Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL),具有一定的參考價值,感興趣的可以了解一下
    2021-07-07
  • Java String不可變性實現(xiàn)原理解析

    Java String不可變性實現(xiàn)原理解析

    這篇文章主要介紹了Java String不可變性實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04

最新評論