0

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:

  1. User opens application (login page where stored data should be displayed)
  2. Data is displayed
  3. User logs in
  4. User logs out
  5. 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;
}

}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
leostiw
  • 1,125
  • 3
  • 12
  • 28
  • 1
    can [this](http://stackoverflow.com/questions/8072311/illegalstateexception-cannot-create-a-session-after-the-response-has-been-commi) or [this](http://stackoverflow.com/questions/11877420/jsf-error-illegalstateexception-pwc3999-cannot-create-a-session-after-the-re) help ? – AllTooSir Jul 17 '13 at 08:11
  • 1
    As explained by BalusC in his both answers, the problem is not about `@ApplicationScoped` bean but an issue with Mojarra impl. Just upgrade the JSF version you're working with or add more details about your problem. – Luiggi Mendoza Jul 17 '13 at 08:14
  • @LuiggiMendoza Ok, so I added javax.faces-2.1.24 and the mojarra version is still 2.1.7. Has it something to do with jboss? – leostiw Jul 17 '13 at 08:42
  • 1
    Yes, JBoss is using its own JSF version. Check more about this [here](https://community.jboss.org/thread/171282) and [here](http://stackoverflow.com/q/11493884/1065197) – Luiggi Mendoza Jul 17 '13 at 08:50
  • Thank you @LuiggiMendoza . I followed the instructions in the 2nd link and it works flawless. – leostiw Jul 17 '13 at 09:21
  • 2
    This is a better JBoss upgrade instruction ;) http://stackoverflow.com/questions/17085717/upgrade-and-use-mojarra-as-the-jsf-implementation-with-jboss-server/17085879#17085879 – BalusC Jul 17 '13 at 12:07
  • @BalusC Indeed, much more specific. Thanks. – leostiw Jul 17 '13 at 13:11

0 Answers0