Users Online website statistics

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

problem:

       How to own a website user statistics (where users include tourist) numbers online?

analysis:

 First of all, I met a problem like the idea at first analysis.

  •   What technology can monitor user access to the server? ( Listeners )
  •   Use those techniques, real-time storage server every time the number of users landing?   (Java four domain object)
  •   Those with technology that allows users to display the number of pages to the client?  (El expression)

If I raise the above knowledge, you are not very understanding, it is recommended to learn at their own Baidu. I only had a relatively small demo, a train of thought.

In the code, there is a very clear presentation.

core content:

The directory structure of the project are as follows:

First create a javaweb project, clear project structure in the figure. (1 represents the project name, 3.jar package basic package (el expressions official website to download it), this is a .jar package Download ( https://download.csdn.net/download/longyanchen/ 11,250,452 )), precautions need to build the project on their own server, deploy the project directly to the server on it. Project preparation environment, I will not describe. (In order to prevent some brothers, put the bag in the wrong direction, I will go all pass)

InitServletContexListener.java(服务器初始化):
​
package com.cyl.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) {

    }
}

​
OnlineNumberHttpSessionListener.java(用户现在监听):
​
package com.cyl.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);
    }
}

​

the index.jsp (display the number of users):

​
<%--
  Created by IntelliJ IDEA.
  User: cyl
  Date: 2019/6/19 0019
  Time: 21:51
  To change this template use File | Settings | File Templates.
--%>
<%@ 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>

​

 

 

 

Guess you like

Origin blog.csdn.net/longyanchen/article/details/93019871