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 :当前在线人数和历史访问量内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

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

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

    这篇文章主要介绍了Idea 快速生成方法返回值的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • springboot常用语法库的基本语法

    springboot常用语法库的基本语法

    FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具,这篇文章主要介绍了springboot常用语法库的基本语法,需要的朋友可以参考下
    2022-12-12
  • Mybatis实现自定义的typehandler三步曲

    Mybatis实现自定义的typehandler三步曲

    这篇文章主要介绍了Mybatis实现自定义的typehandler三步曲的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-07-07
  • Java中Spring的创建和使用详解

    Java中Spring的创建和使用详解

    这篇文章主要介绍了Java中Spring的创建和使用详解,Spring 是⼀个包含了众多⼯具⽅法的 IoC 容器,既然是容器那么 它就具备两个最基本的功能,将对象存储到容器中,从容器中将对象取出来,需要的朋友可以参考下
    2023-08-08
  • SpringBoot解决jar包冲突的问题,简单有效

    SpringBoot解决jar包冲突的问题,简单有效

    这篇文章主要介绍了SpringBoot解决jar包冲突的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 利用gson将map转为json示例

    利用gson将map转为json示例

    这篇文章主要介绍了利用gson将map转为json示例,需要的朋友可以参考下
    2014-05-05
  • 三步轻松搭建springMVC框架

    三步轻松搭建springMVC框架

    这篇文章主要教大家三步轻松搭建springMVC框架,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • SpringBoot拦截器的配置使用介绍

    SpringBoot拦截器的配置使用介绍

    拦截器可以用来实现未满足某些条件,不容许访问某些资源。SpringBoot 支持拦截器,本文主要介绍拦截器的使用与原理
    2022-10-10
  • Spring注解之@Conditional使用解析

    Spring注解之@Conditional使用解析

    这篇文章主要介绍了Spring注解之@Conditional使用解析,@Conditional注解可以说是SpringBoot的条件注解,表示组件只有在所有指定条件都匹配时才有资格注册,条件是可以在 bean 定义注册之前​​以编程方式确定的任何状态,需要的朋友可以参考下
    2024-01-01
  • SpringBoot使用Mybatis注解实现分页动态sql开发教程

    SpringBoot使用Mybatis注解实现分页动态sql开发教程

    这篇文章主要为大家介绍了SpringBoot使用Mybatis注解实现分页及动态sql开发教程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-03-03

最新评论