Adding cookie in JSF PhaseListener

2019-08-21 04:13发布

问题:

I have a legacy application using JSF version 1.1

I am trying to set a cookie in all responses, but through a PhaseListener implementation instead of usual Filter because of specific requirement.

I did something like:

public class MyPhaseListener implements PhaseListener {

    public PhaseId getPhaseId() {
        return PhaseId.ANY_PHASE;
    }

    public void beforePhase(PhaseEvent event) {
    }

    public void afterPhase(PhaseEvent event) {
        if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
            HttpServletResponse httpResponse = (HttpServletResponse) FacesContext
                            .getCurrentInstance().getExternalContext().getResponse();
            int cookieValue = 100;
            Cookie cookie = new Cookie("myCookie", "" + cookieValue);
            cookie.setPath("/");
            httpResponse.addCookie(cookie);
        }
    }
}

However, when I am checking the responses in chrome dev console, I do not see this cookie.

What am I doing wrong?

回答1:

after RENDER_RESPONSE is probably to late, before RENDER_RESPONSE should work fine.



标签: http jsf cookies