原创

jsp 利用 application 统计在线人数

最近看了一些在线统计人数的案例,下面说一下最简单的实现方式是通过jsp利用application统计在线人数。代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <title>application</title>
  </head>
   
  <body>
  <%!
Integer number;
synchronized void numberVisiter()
{
    ServletContext application = getServletContext();
    Integer num = (Integer)application.getAttribute("count");
if(num == null){
    num = new Integer(1);
    application.setAttribute("count",num);
}else{
    num = new Integer(num.intValue() + 1);
    application.setAttribute("count",num);
}
} 
  %>
  <%
  if(session.isNew())
  {
  numberVisiter();
  Integer number = (Integer)application.getAttribute("count");
  }
  %>
  <p>
  <font size="3" color="blue">简单的页面访问计数器</font>
  </p>
  <p>
  <font size="3" color="#000000">
  欢迎访问此页面,您是<%=number%>个访问用户
  </font>
  </p>
  </body>
</html>

本文对于初学者应该有些帮助。

~阅读全文-人机检测~

微信公众号“Java精选”(w_z90110),专注Java技术干货分享!让你从此路人变大神!回复关键词领取资料:如Mysql、Hadoop、Dubbo、Spring Boot等,免费领取视频教程、资料文档和项目源码。微信搜索小程序“Java精选面试题”,内涵3000+道Java面试题!

涵盖:互联网那些事、算法与数据结构、SpringMVC、Spring boot、Spring Cloud、ElasticSearch、Linux、Mysql、Oracle等

评论

分享:

支付宝

微信