web.xml中的会话超时无效

时间:2016-03-22 09:39:36

标签: session jsf session-timeout viewexpiredexception

我是JSF的新手并致力于会话超时,并基于它重定向到注销页面。但它不能正常工作并表现出不寻常的行为,有时它会被重定向,有时却不会......

web.xml代码:

<context-param>
    <param-name>web.TIME_OUT_PAGE</param-name>
    <param-value>/sessionTimeOut.html</param-value>
</context-param>
<listener>
    <display-name>SessionTimeoutNotifier</display-name>
    <listener-class>test.web.SessionTimeoutNotifier</listener-class>
</listener>
<listener>
    <display-name>ViewExpirationListener</display-name>
    <listener-class>test.web.ViewExpirationListener</listener-class>
</listener>
<session-config>
    <session-timeout>30</session-timeout>
</session-config>

和SessionTimeoutNotifier.java的代码:

package test.web;

        import java.io.Serializable;
        import javax.servlet.http.HttpSessionBindingEvent;
        import javax.servlet.http.HttpSessionBindingListener;
        import org.apache.commons.lang.exception.ExceptionUtils;
        import test.User;

        public class SessionTimeoutNotifier implements HttpSessionBindingListener,Serializable
        {

            private static final long serialVersionUID = 8420390506024648336L;

            public SessionTimeoutNotifier(){
            }

            public void valueBound(HttpSessionBindingEvent event)
            {
                User user = (User) event.getSession().getAttribute(User.USER);

                System.out.println("Session Max Inactive Interval Value:"+ event.getSession().getMaxInactiveInterval());

                if (user != null) {
                    System.out.println("Session ID:"+ event.getSession().getId() + " created for user ID: " + user.getId());
                } else {
                    System.out.println("Session ID:"+ event.getSession().getId() + " created for user ID: UNKNOWN");            
                }
            }

            public void valueUnbound(HttpSessionBindingEvent event)
            {
                User user = (User) event.getSession().getAttribute(User.USER);

                System.out.println("Session expired : [" + (user == null ? "Unknown" : user.getId()) + "]");

            }
    }

和ViewExpirationListener.java类:

package test.web;

import java.io.IOException;
import java.util.List;

import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewExpiredException;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

public class ViewExpirationListener implements PhaseListener {

    private static final String TIME_OUT_PAGE_PARAM = "web.TIME_OUT_PAGE";

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.APPLY_REQUEST_VALUES;
    }

    @Override
    public void afterPhase(PhaseEvent event) {
        // Do nothing.
    }

    @Override
    public void beforePhase(PhaseEvent event) {
        FacesContext facesContext = event.getFacesContext();

        List<FacesMessage> iter = facesContext
                .getMessageList(ViewExpiredException.class.getName());

        List<FacesMessage> msgs = facesContext.getMessageList();
        int count = 1;


        if (iter.size() > 0) {
            handleTimeOut(facesContext);
        }

    }

    private void handleTimeOut(FacesContext facesContext) {
        ExternalContext extContext = facesContext.getExternalContext();

        String timeOutPage = extContext.getRequestContextPath()
                + extContext.getInitParameter(TIME_OUT_PAGE_PARAM);

        try {
            extContext.redirect(timeOutPage);
        } catch (IOException e) {
            throw new FacesException(e);
        }

        facesContext.responseComplete();
    }

}

并且在30分钟不活动后仍然没有每次重定向。有时甚至在第31分钟超时工作,有时甚至在5小时后会话仍然有效....

我无法理解我错在哪里以及如何解决它......

提前感谢您的帮助......

0 个答案:

没有答案