使用Servlet获取用户日志

前段时间,实验室需要开发一个用户日志模块,来对实验室的Web项目监控,获取用户的行为日志。个人首先觉得应该主要使用js来实现相关功能,无奈js水平着实太低,最终采用了servlet的方式来实现。

项目介绍

自己先从github上查询到了一个相关项目,clickstream,我先来介绍一下该项目是怎么实现的。
Clickstream的实现

它首先使用了一个Listener来监听ServletContext和HttpSession,代码如下

public class ClickstreamListener implements ServletContextListener, HttpSessionListener {
   

    private static final Log log = LogFactory.getLog(ClickstreamListener.class);

    /** The servlet context attribute key. */
    public static final String CLICKSTREAMS_ATTRIBUTE_KEY = "clickstreams";

    /**
     * The click stream (individual) attribute key: this is
     * the one inserted into the HttpSession.
     */
    public static final String SESSION_ATTRIBUTE_KEY = "clickstream";

    /** The current clickstreams, keyed by session ID. */
    private Map<String, Clickstream> clickstreams = new ConcurrentHashMap<String, Clickstream>();

    public ClickstreamListener() {
        log.debug("ClickstreamLogger constructed");
    }

    /**
     * Notification that the ServletContext has been initialized.
     *
     * @param sce The context event
     */
    public void contextInitialized(ServletContextEvent sce) {
        log.debug("ServletContext initialised");
                //把clickstreams存放于ServletContext中,web容器集群时,不能采用这种方式
        sce.getServletContext().setAttribute(CLICKSTREAMS_ATTRIBUTE_KEY, clickstreams);
    }

    /**
     * Notification that the ServletContext has been destroyed.
     *
     * @param sce The context event
     */
    public void contextDestroyed(ServletContextEvent sce) {
        log.debug("ServletContext destroyed");
        // help gc, but should be already clear except when exception was thrown during sessionDestroyed
        clickstreams.clear();//应该在此处完成持久化
    }

    /**
     * Notification that a Session has been created.
     *
     * @param hse The session event
     */
    public void sessionCreated(HttpSessionEvent hse) {
        final HttpSession session = hse.getSession();
        if (log.isDebugEnabled()) {
            log.debug("Session " + session.getId() + " was created, adding a new clickstream.");
        }

        Object attrValue = session.getAttribute(SESSION_ATTRIBUTE_KEY);
        if (attrValue != null) {
            log.warn("Session " + session.getId() + " already has an attribute named " +
                    SESSION_ATTRIBUTE_KEY + ": " + attrValue);
        }

        final Clickstream clickstream = new Clickstream();
                //为新建的session绑定一个clickstream
        session.setAttribute(SESSION_ATTRIBUTE_KEY, c
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值