java 从 ServletRequest 获取 Cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5417492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:01:13  来源:igfitidea点击:

Get Cookies from ServletRequest

javajsfservletscookiesservlet-listeners

提问by Ben

I'm using ServletRequestListenerto attach to new requests, get a ServletRequestobject and extract cookies from it.

我正在使用ServletRequestListener附加到新请求,获取一个ServletRequest对象并从中提取 cookie。

I've noticed that only HTTPServletRequesthas cookies but I haven't found a connection between those two objects.

我注意到只有HTTPServletRequestcookie,但我还没有找到这两个对象之间的联系。

Is it okay to use

可以用吗

HttpServletRequest request = ((HttpServletRequest) FacesContext.getCurrentInstance()
                .getExternalContext().getRequest());

to retrieve the request while in a RequestInitializedmethod? (I do want to run on every request)

RequestInitialized方法中检索请求?(我确实想在每个请求上运行)

FYI - This is all done in a JSF 1.2 Application

仅供参考 - 这一切都在 JSF 1.2 应用程序中完成

回答by BalusC

This is not correct. The FacesContextisn't available in a ServletRequestListenerper se. The getCurrentInstance()might return null, leading to NPE's.

这是不正确的。该FacesContext不是一个可用的ServletRequestListener本身。将getCurrentInstance()可能会返回null,导致NPE的。

If you're running the webapp on a HTTP webserver (and thus not some Portlet webserver for example), you could just cast the ServletRequestto HttpServletRequest.

如果你正在运行在HTTP Web服务器(因此不是一些Portlet的Web服务器为例)的Web应用程序,你可以只投的ServletRequestHttpServletRequest

public void requestInitialized(ServletRequestEvent event) {
    HttpServletRequest request = (HttpServletRequest) event.getServletRequest();
    // ...
}


Note that a more common practice is to use a Filterfor this since you can map this on a fixed URL pattern like *.jsfor even on specific servlets so that it runs only when the FacesServletruns. You might for example want to skip cookie checks on static resources like CSS/JS/images.

请注意,更常见的做法是为此使用 a Filter,因为您可以将其映射到固定的 URL 模式,例如*.jsf甚至特定的 servlet,以便它仅在FacesServlet运行时运行。例如,您可能希望跳过对 CSS/JS/图像等静态资源的 cookie 检查。

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
    HttpServletRequest request = (HttpServletRequest) req;
    // ...
    chain.doFilter(req, res);
}


When you happens to be already inside the JSF context (in a managed bean, phaselistener or whatever), you could just use ExternalContext#getRequestCookieMap()to get the cookies.

当您碰巧已经在 J​​SF 上下文中(在托管 bean、阶段侦听器或其他任何东西中)时,您可以使用ExternalContext#getRequestCookieMap()来获取 cookie。

Map<String, Object> cookies = externalContext.getRequestCookieMap();
// ...

When running JSF on top of Servlet API, the map value is of type javax.servlet.http.Cookie.

在 Servlet API 之上运行 JSF 时,映射值的类型为javax.servlet.http.Cookie

Cookie cookie = (Cookie) cookies.get("name");

回答by nfechner

Yes, you can do that. In Web scenarios, this will always be ok. If you want to be sure, you could do a check for the type first. (Good practice anyway):

是的,你可以这样做。在 Web 场景中,这总是没问题的。如果你想确定,你可以先检查一下类型。(无论如何都是好习惯):

if (FacesContext.getCurrentInstance().getExternalContext().getRequest() instanceof HttpServletRequest) {
...

By the way: Why do you have to use FacesContext? From where are you calling this code?

顺便说一句:你为​​什么要使用FacesContext?你从哪里调用这个代码?