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

网友投稿 307 2022-11-28


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配置(

com.cyl.count.InitServletContexListener

*/

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" %>

历史访问量

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(" \r\n" +

"\r\n" +

"

"\r\n" +

wvkHC "

"\r\n" +

"

out.print("

out.print("您是第 "+ servletContext.getAttribute("count")+"位访客");

out.print("

out.print("\r\n" +

"");

}

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletExceptionwvkHC, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

}

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%

//统计网页访问量

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));

%>

欢迎您访问,本页面已经被访问过 <%=application.getAttribute("count")%>

总结


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Boot设置支持跨域请求过程详解
下一篇:Maven访问仓库顺序代码实例解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~