统计在线人数解决方法

统计在线人数
我的代码:
//数据库存储
public class StatDaoImpl implements StatDao{
    
    private JdbcTemplate template;
    
    public void setDataSource(DataSource ds){
        template = new JdbcTemplate(ds);
    }

    public int getTotalVisit() {
        String sql = "select total from custom_total_stat where id=1";
        return template.queryForInt(sql);
    }

    public void updateTotalVisit(int total) {
        String sql = "update custom_total_stat set total=? where id=1";
        template.update(sql, total);
    }
}
//统计类
public class StatisticsListener implements HttpSessionListener {
    
    //public static int today = 0;//今日访问次数
    
    private int curr = 0;//当前在线人数
    
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext application = se.getSession().getServletContext();
        if(application.getAttribute("total")==null){
         application.setAttribute("total", new Integer(0));
        }
        int total = (Integer) application.getAttribute("total");
        total++;
        //today++;
        curr++;
        application.setAttribute("total", total);
        application.setAttribute("curr", curr);
        //application.setAttribute("today", today);
    }
    
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext application = se.getSession().getServletContext();
        curr--;
        application.setAttribute("curr", curr);
    }
    
}

//jsp页面

<%@ page pageEncoding="UTF-8" contentType="text/html; charset=utf-8"%>
<%@ page session= "true" %>
    <%   
        //out.println("alert(111);");
response.setDateHeader("Expires", 0);    
response.setHeader("Cache-Control", "no-cache");    
response.setHeader("Pragma", "no-cache"); 
        int curr = (Integer)application.getAttribute("curr");
int total = (Integer)application.getAttribute("total");
out.println("document.write('您是第<em>')");
out.println("document.write('"+total+"')");
out.println("document.write('位访客&nbsp;&nbsp;&nbsp;<em>')");
out.println("document.write('</em></span><span>当前有<em>')");
out.println("document.write('"+curr+"')");
out.println("document.write('</em>人在线 ')");
    %>
如何将这三段代码联合起来,目的是:将统计的在线人数存储到数据库中。谢谢大家
------解决方案--------------------
在线人数不应该是实时变动的吗,为什么还要存到数据库,要存也是某时刻的在线人数。
1、可以根据ip地址
2、可以用户登录,但是要保证能及时知道用户是否退出

------解决方案--------------------
统计在线人数解决方法
------解决方案--------------------
你这个需求跟我同事目前需要实现的几乎一样啊,他打算写一个日志,然后查询日志呢
------解决方案--------------------
你可以让用户先进action,在action里面对数据库访问次数做修改,在到页面这样就也可以知道访问了多少次了