Search code examples
javaspringspring-mvchttpsession

Spring HttpSessionListener falls in loop


I create a HttpSessionListener in my project. in this listener, I set something in session like below:

 public void sessionCreated(HttpSessionEvent se) {
    //some Business for access to subsiteId
    se.getSession().setAttribute("subsiteId", subsiteId);

    //set some atribute for Statistic Model
    iStatisticService.save(remoteIp, userAgent, page); 
 }

in my GenericSave (that my StatisticService extend it), I want to read subsiteId Attribute that I set already in SessionListener:

((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getAttribute("subsiteId")

But my app falls in loop. and come back to my Listener again! I guess me access to Session before it completely created. any Idea?


Solution

  • Finally, I solve this problem by set attribute of Statistic Model in sessionCreated and save Statistic Model in sessionDestroyed:

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        //set some atribute for Statistic Model
    }
    
    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        //save Statistic Model
    }