019.获取每天该项目创建了多少个HttpSession对象

 

 

 

 

 

 

package com.item.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * Druid连接池的工具类
 */
public class JDBCUtils
{

    //1.定义成员变量 DataSource
    private static DataSource ds;

    static {
        try {
            //1.加载配置文件
            Properties pro = new Properties();
            pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("com/item/properties/druid.properties"));
            //2.获取DataSource
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     */
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    /**
     * 释放资源
     */
    public static void close(Statement stmt, Connection conn) {

        close(null, stmt, conn);
    }


    public static void close(ResultSet rs, Statement stmt, Connection conn) {


        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();//归还连接
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取连接池(数据源)方法
     */

    public static DataSource getDataSource() {
        return ds;
    }

}
package com.item.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;

public class UpfateDate
{
    public static PreparedStatement preparedStatement = null;
    public static Connection connection = null;

    /**
     * 1.向数据库中写入数据
     */
    public static void updataOrInsert(int onlineCounter) throws SQLException
    {
        connection = JDBCUtils.getConnection();
        String sql = "insert into onlinecounter (onlineCounter,`time`) values (?,?)".trim();
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, onlineCounter);
        preparedStatement.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
        int i = preparedStatement.executeUpdate();
        if (i > 0)
        {
            System.out.println("插入数据Successful!!!");
        }
        else
        {
            System.out.println("插入数据Fail!!!");
        }
        JDBCUtils.close(preparedStatement, connection);
    }


}
package com.item.listener;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.item.utils.UpfateDate;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.sql.SQLException;
import java.util.Calendar;

@WebListener
public class OnlineCounterListener implements HttpSessionListener
{
    //定义一个计数的变量,只要有HttpSession创建就会+1
    public static int onlineCounter = 0;

    public static  DateTime dateOld = DateUtil.date();

    @Override
    public void sessionCreated(HttpSessionEvent se)
    {
        /**
         * 1.获取日期的时间查
         * 2.获取明天
         * 3.获取今天
         *4.创建一个全局变量来存储日期
         */

        try
        {
            DateTime dateNew = DateUtil.date();//永远获取当前新的日期
            long betweenDay = DateUtil.between(dateOld, dateNew, DateUnit.DAY);
            if (betweenDay == 0)
            {
                UpfateDate.updataOrInsert(onlineCounter++);
            }
            else
            {
                onlineCounter=0;
                dateOld=dateNew;
                UpfateDate.updataOrInsert(onlineCounter++);
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }


    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se)
    {

    }
}

 

posted @ 2021-09-01 15:13  李林林  阅读(100)  评论(0编辑  收藏  举报