I have an @ApplicationScoped bean where I store some data from my DB. Its the same data for all users, thats why I decided to place it in a @ApplicationScope bean.
Following aspect though:
- User opens application (login page where stored data should be displayed)
- Data is displayed
- User logs in
- User logs out
- Navigate to login-page (stored data should be displayed)
After logout i get following error (Cannot create a session after the response has been committed):
10:02:37,284 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/kundenportal].[Faces Servlet]] (http-localhost-127.0.0.1-8080-5) Servlet.service() for servlet Faces Servlet threw exception: java.lang.IllegalStateException: Cannot create a session after the response has been committed
at org.apache.catalina.connector.Request.doGetSession(Request.java:2636) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.Request.getSession(Request.java:2375) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:841) [jbossweb-7.0.13.Final.jar:]
at com.sun.faces.context.ExternalContextImpl.getSession(ExternalContextImpl.java:155) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.renderkit.ServerSideStateHelper.writeState(ServerSideStateHelper.java:175) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.renderkit.ResponseStateManagerImpl.writeState(ResponseStateManagerImpl.java:122) [jsf-impl-2.1.7-jbossorg-2.jar:]
This is the logout method:
public String logout() {
    ((HttpServletRequest) FacesContext.getCurrentInstance()
            .getExternalContext().getRequest()).getSession(false)
            .invalidate();
    return "loginFAILED"; // navigate to login-page
}
I get same error when trying to login with wrong credentials. In fact, if the credentials are wrong, i call the logout method(invalidate, navigate again to login page). Additionally, a growl-message should be displayed. If I want to try another login, I have to refresh the page, because nothing happens if I click on the login button.
This is my @ApplicationScoped bean:
@Named
@ApplicationScoped
public class NewsController extends BaseController implements Serializable {
private static final long serialVersionUID = 1L;
@PersistenceContext
protected EntityManager em;
private List<News> newsList;
@PostConstruct
public void init() {
    getAvailableNews();
}
public void getAvailableNews() {
    if (newsList == null) {
        newsList = new LinkedList<News>();
        Query q = em.createNativeQuery(
                "SELECT * FROM news WHERE sysdate BETWEEN ab AND bis+1",
                News.class);
        @SuppressWarnings("unchecked")
        List<News> result = q.getResultList();
        for (News n : result)
            newsList.add(n);
    }
}
public List<News> getNewsList() {
    return newsList;
}
public void setNewsList(List<News> newsList) {
    this.newsList = newsList;
}
}
 
    