我们如何从给定的路径获取 cookie?当我们执行 request.getCookies() 时,默认情况下它从“\”中提取 cookie。但如果我想从给定路径获取它,假设“\bin\test”。在这种情况下我们应该怎么做?
提前致谢
您需要按路径滚动您自己的 cookie 过滤器
http://docs.oracle.com/javaee/1.4/api/javax/servlet/http/HttpServletRequest.html?is-external=true#getCookies()
http://docs.oracle.com/javaee/5/api /javax/servlet/http/Cookie.html
在 Java Servlet 中如何更改现有 cookie 的值?
List<Cookie> getCookiesFromPath(@Nonnull SlingHttpServletRequest request, String path) {
Cookie[] allCookies = request.getCookies();
if (path == null || path.isEmpty()) { // convert cookie array to cookie list
return Arrays.asList(allCookies);
}
List<Cookie> cookieList = new ArrayList<Cookie>();
for (Cookie c : allCookies) {
if (c.getPath().equals(path)) { // or equals()
cookieList.add(c);
}
}
return cookieList;
}