在JSF PhaseListener中添加cookie

时间:2019-02-03 06:37:51

标签: http jsf cookies

我有一个使用JSF 1.1版的旧版应用程序

我正尝试在所有响应中设置cookie,但是由于特定要求,因此通过PhaseListener实现而不是通常的Filter设置

我做了类似的事情:

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);
        }
    }
}

但是,当我在chrome开发者控制台中检查响应时,没有看到此cookie。

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

在RENDER_RESPONSE之后可能要晚了,而在RENDER_RESPONSE应该可以正常工作之前。