0

The following error message is shown when an action method of a <h:commandLink/> JSF tag is called:

PWC3999: Cannot create a session after the response has been committed

The meaning of the error message is very straightforward, but my question is how the response could have been committed before the action method finishes execution.

(This error is only occurring time to time, not every time the commandLink is clicked.)

I use
JSF implementation: Mojarra V2.1.3
JSF component library: Primefaces V2.2.1
Server: GlassFish Open Source Edition V3.1.1 (build 12)

EDIT : I attach some codes bellow, if this assist in getting the solution:

The markup:

<h:commandLink value="[Log in]"
                               action="#{headerAndFooterTemplateBacking.loginFilter}"
                               disabled="#{sessionScope.pk > 0 ? true : false}"
                               styleClass="#{sessionScope.pk > 0 ? 'disabled' : 'notDisabled'} padding" immediate="true"/>

The action method:

public void loginFilter() {
           String from = FacesContext.getCurrentInstance().getViewRoot().getViewId();

        int pk = getSessionMap().get("pk") == null ? -1 : (Integer) (getSessionMap().get("pk"));

        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Object selected = request.getParameter("selected");
        if (pk <= 0) {
            if (selected != null) {
                getSessionMap().put("from", from + "?faces-redirect=true&selected=" + selected);
            } else {
                getSessionMap().put("from", from + "?faces-redirect=true");
            }

            getFacesContext().getApplication().getNavigationHandler().
                    handleNavigation(getFacesContext(), null,
                    "/pages/login/login.xhtml?faces-redirect=true");
        }

    }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
siva636
  • 16,109
  • 23
  • 97
  • 135

2 Answers2

2

This is a bug in Mojarra 2.1.x: issue 2215. The session creation is postponed as much as possible to avoid unnecessary session creation. However they didn't take into account that the session needs to be created before the response is committed. The response will be auto-committed whenever the page is larger than the response's default buffer size.

You can find several workarounds in Adding <h:form> causes java.lang.IllegalStateException: Cannot create a session after the response has been committed.


Unrelated to the concrete problem: you should be using a session scoped bean instead of manually fiddling with the session map.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • When I see the name "BalusC" in a post, I get confident of getting a proper full solution. Thanks again. – siva636 Nov 18 '11 at 13:28
0

This can happen if you are calling ajax (update) to render the page and that page is already rendered. Also if you update something, which value is changing dynamically.

Can be of a lot of reasons, so YES, the code is important.

spauny
  • 4,976
  • 9
  • 44
  • 61
  • @MISS_DUKE What browser are you using? IE has some issues with what are you trying to do. Also as a tip, if you're using Primefaces you could also use it's components (example p:commandLink) – spauny Nov 18 '11 at 09:22
  • "if you're using Primefaces you could also use it's components (example p:commandLink)" If h:commandLink is enough for me why should I use PF equivalent? – siva636 Nov 18 '11 at 09:52
  • Why do you use PF if JSF it's enough for you?! – spauny Nov 18 '11 at 09:55
  • 1
    Use PF as an extension (but not replacement) to JSF, when a feature in core JSF is enough it is not advisable to replace it with PF (or any other JSF library) – siva636 Nov 18 '11 at 10:16