Adding cookie in JSF PhaseListener

2019-08-21 03:48发布

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?

标签: http jsf cookies
1条回答
孤傲高冷的网名
2楼-- · 2019-08-21 04:33

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

查看更多
登录 后发表回答