IDEA 当前在线人数和历史访问量的示例代码

 更新时间:2020年08月07日 14:18:51   作者:柴大大  
这篇文章主要介绍了IDEA 当前在线人数和历史访问量的实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

当前在线人数

一共需要三处

创建监听器

package com.count;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/*
 初始化:
   只有服务器的启动,才会创建servletContext对象。
  用于监听servletContext创建,一旦创建servletContext创建,则设置servletContext中的count值为0;
*/
@WebListener
/*
 这个注解的作用是启动监听,相当于在web.xml配置(
 <listener>
  <listener-class>com.cyl.count.InitServletContexListener</listener-class>
 </listener>
*/
public class InitServletContexListener implements ServletContextListener {
 @Override
 public void contextInitialized(ServletContextEvent servletContextEvent) {
  //获取ServletContext域对象
  ServletContext servletContext = servletContextEvent.getServletContext();
  //给ServletContext域对象,设置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;
 
/**
 * @监听在线人数,监听session的创建和销毁
 *  如果session创建 获取ServletContext中的count++,重新设置
 *  如果session销毁 获取ServletContext中的count--,重新设置
 */
@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>当前在线人数:${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 {
  //设置字符编码
  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>登陆网页次数统计</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>
 <%
 //统计网页访问量
 if (application.getAttribute("count") == null) {
  application.setAttribute("count", 0);//application.setAttribute("count", new Integer(0));
 }
 Integer count = (Integer) application.getAttribute("count");
 //使用application对象读取count参数的值,再在原值基础上累加1
 application.setAttribute("count", count + 1);//application.setAttribute("count", new Integer(count.intValue() + 1));
 %>
 <h2>
 <!-- 输出累加后的count参数对应的值 -->
 欢迎您访问,本页面已经被访问过 <font color="#ff0000"><%=application.getAttribute("count")%></font>次
 </h2>
 </body>
</html>

总结

到此这篇关于IDEA :当前在线人数和历史访问量的文章就介绍到这了,更多相关IDEA :当前在线人数和历史访问量内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java 包装类型及易错陷阱详解

    Java 包装类型及易错陷阱详解

    这篇文章主要介绍了Java 包装类型及易错陷阱详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • mybatisPlus返回Map类型的集合

    mybatisPlus返回Map类型的集合

    本文主要介绍了mybatisPlus返回Map类型的集合,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • SpringBoot中@ConfigurationProperties实现配置自动绑定的方法

    SpringBoot中@ConfigurationProperties实现配置自动绑定的方法

    本文主要介绍了SpringBoot中@ConfigurationProperties实现配置自动绑定的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • 详解Java中native方法的使用

    详解Java中native方法的使用

    native是与C++联合开发的时候用的!使用native关键字说明这个方法是原生函数,也就是这个方法是用C/C++语言实现的,并且被编译成了DLL,由java去调用。本文给大家介绍java 中native方法使用,感兴趣的朋友一起看看吧
    2020-09-09
  • Java详细讲解堆排序与时间复杂度的概念

    Java详细讲解堆排序与时间复杂度的概念

    本文主要介绍了java实现堆排序以及时间复杂度,堆排序这种排序算法是我们经常用到的,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Java将CSV的数据发送到kafka的示例

    Java将CSV的数据发送到kafka的示例

    这篇文章主要介绍了Java将CSV的数据发送到kafka得示例,帮助大家更好得理解和使用Java,感兴趣的朋友可以了解下
    2020-11-11
  • java用list集合存储学生信息并算出成绩平均值操作

    java用list集合存储学生信息并算出成绩平均值操作

    这篇文章主要介绍了java用list集合存储学生信息并算出成绩平均值操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • 使用sts工具、SpringBoot整合mybatis的详细步骤

    使用sts工具、SpringBoot整合mybatis的详细步骤

    这篇文章主要介绍了使用sts工具、SpringBoot整合mybatis的详细步骤,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • 详解Java对象的内存布局

    详解Java对象的内存布局

    这篇文章主要介绍了Java对象的内存布局,对对象内存感兴趣的同学,一定要仔细研究下
    2021-04-04
  • 基于spring security实现登录注销功能过程解析

    基于spring security实现登录注销功能过程解析

    这篇文章主要介绍了基于spring security实现登录注销功能过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01

最新评论