Handling 'session expired' in JSF web application, running in JBoss AS 5 [duplicate]

荒凉一梦 提交于 2019-11-29 23:08:58
Chris Dale

The following approach works for me. Note that you have to use the JSTL core taglib redirect and not the jsp redirect in order for this to work (as the jsp also expires).

In your FacesConfig.xml you put the following:

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

sessionExpired.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:redirect url="/login.jsf" />

You can also use this approach for other error types or exceptions. For example the element contains a mapping between an error code or exception type and the path of a resource in the web application.:

<error-page>
    <error-code>400</error-code>
    <location>/400.html</location>
</error-page>

or element contains a fully qualified class name of a Java exception type.

<error-page>
    <exception-type>javax.servlet.ServletException</exception-type>
    <location>/servlet/ErrorDisplay</location>
</error-page>

If you are using Mojarra/Sun RI you might want to try to add this to your web.xml:

<context-param>
    <param-name>com.sun.faces.enableRestoreView11Compatibility</param-name> 
    <param-value>true</param-value>
</context-param>

However be aware that this isn't always the perfect solution. It hides the fact that the user has lost its session.

Implement javax.faces.event.PhaseListener for Restore view

@Override
public void afterPhase(PhaseEvent event) {
    FacesContext facesContext = event.getFacesContext();
     if(facesContext.getViewRoot()==null){   
       try{   
           facesContext.getExternalContext().redirect(HOME_PAGE);   
           facesContext.responseComplete();   
       } catch (IOException e){   
           e.printStackTrace();   
       }   
     }
}

@Override
public void beforePhase(PhaseEvent event) {}

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

register in faces-config.xml

I would suggest writing a session listener in conjunction with the filter.

When the session expires you can create a new session object and set a timeout value on the new object.

Just check for the timeout value in the filter and redirect the browser.

See http://www.java2s.com/Code/Java/Servlets/Servletsessionlistener.htm

ifti

I tried to write a filter for it but some how it was not working for me, so I made an alternate for it.

I did it like this in every page that I don't want the user to access without Login:

<f:view>
    <h:dataTable value="#{userHome.validuser()}"/>
     // my code
<f:view/>

This will call the function validuser() which is in my session managed bean.

Now this is my function. During login I already insert the user object into the session.

public void validuser()
{
     FacesContext context = FacesContext.getCurrentInstance();
    UserLogin ul = (UserLogin) context.getExternalContext().getSessionMap().get("userbean");

     if (ul == null)
         try{
                context.getExternalContext().redirect("/HIBJSF/faces/LoginPage.xhtml");
                context.responseComplete();
        }
         catch (IOException e)
        {
         e.printStackTrace();
        }
} 

If there is a session but no one had logged in, then it will take you to a redirect page.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!