Java EE过滤器无法获取cookie?

时间:2021-07-17 01:59:20

Why aren't cookies able to be referenced from a servlet filter? It just seems beyond me that Java EE wouldn't allow you to sanitize cookie values:

为什么不能从servlet过滤器引用cookie?我认为Java EE不允许您清理cookie值:

public void doFilter(ServletRequest request, ServletResponse response, 
                             FilterChain chain) 
                             throws ServletException, IOException {
    request.
}

ServletRequest does not support getCookies (as is the case with HttpServletRequest).

ServletRequest不支持getCookies(与HttpServletRequest一样)。

3 个解决方案

#1


In order to get the cookies you need to cast it to an HttpServletRequest.

为了获取cookie,您需要将其转换为HttpServletRequest。

HttpServletRequest httpReq = (HttpServletRequest) request;

The reason that ServletResponse class doesn't support cookies is because the protocol isn't necessarly http in a ServletRequest, you can't be sure there are Cookies. Cookies are an Http thing.

ServletResponse类不支持cookie的原因是因为在ServletRequest中协议不一定是http,你不能确定是否有Cookie。 Cookies是一个Http的东西。

#2


Servlets aren't required to be accessed via the HTTP protocol. Therefore, your servlet does not have to be an HttpServlet - it may be a servlet that sends out specific documents via FTP, for example. Because of this, the basic properties of a servlet are encapsulated in the ServletRequest and ServletResponse interfaces, but if you know that your servlet is an HTTPServlet, you may downcast these to HttpServletRequest and HttpServletResponse respectively with no chance of a ClassCastException as long as your Servlet is truly an HttpServlet.

不需要通过HTTP协议访问Servlet。因此,您的servlet不必是HttpServlet - 例如,它可能是通过FTP发送特定文档的servlet。因此,servlet的基本属性封装在ServletRequest和ServletResponse接口中,但是如果你知道你的servlet是一个HTTPServlet,你可以将它们分别转发到HttpServletRequest和HttpServletResponse,只要你的Servlet没有ClassCastException的可能性。是一个真正的HttpServlet。

#3


You do know that you can actually cast it to HttpServletRequest, right? :-)

你知道你实际上可以将它转换为HttpServletRequest,对吗? :-)

#1


In order to get the cookies you need to cast it to an HttpServletRequest.

为了获取cookie,您需要将其转换为HttpServletRequest。

HttpServletRequest httpReq = (HttpServletRequest) request;

The reason that ServletResponse class doesn't support cookies is because the protocol isn't necessarly http in a ServletRequest, you can't be sure there are Cookies. Cookies are an Http thing.

ServletResponse类不支持cookie的原因是因为在ServletRequest中协议不一定是http,你不能确定是否有Cookie。 Cookies是一个Http的东西。

#2


Servlets aren't required to be accessed via the HTTP protocol. Therefore, your servlet does not have to be an HttpServlet - it may be a servlet that sends out specific documents via FTP, for example. Because of this, the basic properties of a servlet are encapsulated in the ServletRequest and ServletResponse interfaces, but if you know that your servlet is an HTTPServlet, you may downcast these to HttpServletRequest and HttpServletResponse respectively with no chance of a ClassCastException as long as your Servlet is truly an HttpServlet.

不需要通过HTTP协议访问Servlet。因此,您的servlet不必是HttpServlet - 例如,它可能是通过FTP发送特定文档的servlet。因此,servlet的基本属性封装在ServletRequest和ServletResponse接口中,但是如果你知道你的servlet是一个HTTPServlet,你可以将它们分别转发到HttpServletRequest和HttpServletResponse,只要你的Servlet没有ClassCastException的可能性。是一个真正的HttpServlet。

#3


You do know that you can actually cast it to HttpServletRequest, right? :-)

你知道你实际上可以将它转换为HttpServletRequest,对吗? :-)