开发者

How to redirect to index page if session time out happened in jsf application

开发者 https://www.devze.com 2022-12-24 21:01 出处:网络
I am using JSF RI开发者_C百科 1.1. How to redirect to index page if session time out happens?There are two ways which can be combinied:

I am using JSF RI开发者_C百科 1.1. How to redirect to index page if session time out happens?


There are two ways which can be combinied:

  1. Make use of the <meta> refresh header in the HTML <head> element in combination with HttpSession#getMaxInactiveInterval() which returns the remnant of seconds the session has yet to live.

    <meta http-equiv="refresh" content="${pageContext.session.maxInactiveInterval};url=index.jsf">
    

    This approach will automatically redirect the page to the given url when the session expires.

  2. Catch ViewExpiredException in web.xml:

    <error-page>
        <exception-type>javax.faces.application.ViewExpiredException</exception-type>
        <location>/index.jsf</location>
    </error-page>
    

    This approach will automatically forward the request to the given <location> when a POST request has been fired (h:commandButton, h:commandLink, etc) while the session is expired.

Note that I personally would prefer an intermediate "Session Expired" warning page or alert to avoid "wtf?" experiences and thus improve the user experience. Even more, I would as well prefer firing an ajaxical poll every minute when the client has shown activity by listening on click and keypress, so that the session's lifetime can be postponed more.


JSF2:

<meta http-equiv="refresh" content="#{facesContext.externalContext.sessionMaxInactiveInterval};url=#{request.contextPath}/index.xhtml"/>


You can use a Filter to catch the particular exception indicating the timeout, and redirect from there.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号