最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

一共需要三處

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

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)聽(tīng)servletContext創(chuàng)建,一旦創(chuàng)建servletContext創(chuàng)建,則設(shè)置servletContext中的count值為0;
*/
@WebListener
/*
 這個(gè)注解的作用是啟動(dòng)監(jiān)聽(tīng),相當(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)聽(tīng)在線人數(shù),監(jiān)聽(tīng)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>
<h1>當(dāng)前在線人數(shù):${count}</h1>
</body>
</html>

歷史訪問(wèn)量

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ì)象為空 ,說(shuō)明是第一次訪問(wèn),并將count,放入servletCount
  if( servletContext.getAttribute("count") == null) {
   count = 1;
   servletContext.setAttribute("count", count);
  }else {
   //否則就不是第一次訪問(wèn),將登陸的計(jì)數(shù)器進(jìn)行加1的數(shù)據(jù)更新
   servletContext.setAttribute("count", count+1);
  }
 
  //將登陸的次數(shù)顯示在頁(yè)面上
  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)頁(yè)次數(shù)統(tǒng)計(jì)</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)計(jì)網(wǎng)頁(yè)訪問(wèn)量
 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));
 %>
 <h2>
 <!-- 輸出累加后的count參數(shù)對(duì)應(yīng)的值 -->
 歡迎您訪問(wèn),本頁(yè)面已經(jīng)被訪問(wèn)過(guò) <font color="#ff0000"><%=application.getAttribute("count")%></font>次
 </h2>
 </body>
</html>

總結(jié)

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

相關(guān)文章

  • Idea 快速生成方法返回值的操作

    Idea 快速生成方法返回值的操作

    這篇文章主要介紹了Idea 快速生成方法返回值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • springboot常用語(yǔ)法庫(kù)的基本語(yǔ)法

    springboot常用語(yǔ)法庫(kù)的基本語(yǔ)法

    FreeMarker 是一款?模板引擎: 即一種基于模板和要改變的數(shù)據(jù), 并用來(lái)生成輸出文本(HTML網(wǎng)頁(yè),電子郵件,配置文件,源代碼等)的通用工具,這篇文章主要介紹了springboot常用語(yǔ)法庫(kù)的基本語(yǔ)法,需要的朋友可以參考下
    2022-12-12
  • Mybatis實(shí)現(xiàn)自定義的typehandler三步曲

    Mybatis實(shí)現(xiàn)自定義的typehandler三步曲

    這篇文章主要介紹了Mybatis實(shí)現(xiàn)自定義的typehandler三步曲的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • Java中Spring的創(chuàng)建和使用詳解

    Java中Spring的創(chuàng)建和使用詳解

    這篇文章主要介紹了Java中Spring的創(chuàng)建和使用詳解,Spring 是?個(gè)包含了眾多?具?法的 IoC 容器,既然是容器那么 它就具備兩個(gè)最基本的功能,將對(duì)象存儲(chǔ)到容器中,從容器中將對(duì)象取出來(lái),需要的朋友可以參考下
    2023-08-08
  • SpringBoot解決jar包沖突的問(wèn)題,簡(jiǎn)單有效

    SpringBoot解決jar包沖突的問(wèn)題,簡(jiǎn)單有效

    這篇文章主要介紹了SpringBoot解決jar包沖突的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 利用gson將map轉(zhuǎn)為json示例

    利用gson將map轉(zhuǎn)為json示例

    這篇文章主要介紹了利用gson將map轉(zhuǎn)為json示例,需要的朋友可以參考下
    2014-05-05
  • 三步輕松搭建springMVC框架

    三步輕松搭建springMVC框架

    這篇文章主要教大家三步輕松搭建springMVC框架,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • SpringBoot攔截器的配置使用介紹

    SpringBoot攔截器的配置使用介紹

    攔截器可以用來(lái)實(shí)現(xiàn)未滿足某些條件,不容許訪問(wèn)某些資源。SpringBoot 支持?jǐn)r截器,本文主要介紹攔截器的使用與原理
    2022-10-10
  • Spring注解之@Conditional使用解析

    Spring注解之@Conditional使用解析

    這篇文章主要介紹了Spring注解之@Conditional使用解析,@Conditional注解可以說(shuō)是SpringBoot的條件注解,表示組件只有在所有指定條件都匹配時(shí)才有資格注冊(cè),條件是可以在 bean 定義注冊(cè)之前??以編程方式確定的任何狀態(tài),需要的朋友可以參考下
    2024-01-01
  • SpringBoot使用Mybatis注解實(shí)現(xiàn)分頁(yè)動(dòng)態(tài)sql開(kāi)發(fā)教程

    SpringBoot使用Mybatis注解實(shí)現(xiàn)分頁(yè)動(dòng)態(tài)sql開(kāi)發(fā)教程

    這篇文章主要為大家介紹了SpringBoot使用Mybatis注解實(shí)現(xiàn)分頁(yè)及動(dòng)態(tài)sql開(kāi)發(fā)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03

最新評(píng)論

郴州市| 自贡市| 长沙市| 额济纳旗| 漳州市| 开鲁县| 双柏县| 遂昌县| 舞钢市| 桦甸市| 思南县| 昆明市| 阿坝县| 孝感市| 张家川| 峡江县| 凭祥市| 平武县| 壤塘县| 伊春市| 曲沃县| 安西县| 潮州市| 浮梁县| 宣武区| 枣阳市| 兴安盟| 洪江市| 通江县| 韩城市| 郎溪县| 乌兰县| 辰溪县| 武乡县| 榕江县| 泸水县| 尚义县| 密山市| 恩平市| 易门县| 南宫市|