Using Spring MVC 3.1+ WebApplicationInitializer to programmatically configure session-config and error-page

后端 未结 5 2135
迷失自我
迷失自我 2020-12-14 18:19

WebApplicationInitializer provides a way to programmatically represent a good portion of a standard web.xml file - the servlets, filters, listeners.

However I have

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 18:30

    Actually WebApplicationInitializer doesn't provide it directly. But there is a way to set sessointimeout with java configuration.

    You have to create a HttpSessionListner first :

    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    
    public class SessionListener implements HttpSessionListener {
    
        @Override
        public void sessionCreated(HttpSessionEvent se) {
            //here session will be invalidated by container within 30 mins 
            //if there isn't any activity by user
            se.getSession().setMaxInactiveInterval(1800);
        }
    
        @Override
        public void sessionDestroyed(HttpSessionEvent se) {
            System.out.println("Session destroyed");
        }
    }
    

    After this just register this listener with your servlet context which will be available in WebApplicationInitializer under method onStartup

    servletContext.addListener(SessionListener.class);
    

提交回复
热议问题