如何从JAVA中的给定路径获取cookie?

时间:2013-08-02 09:17:04

标签: jsp cookies cq5

我们如何从特定路径获取Cookie?当我们执行request.getCookies()时,默认情况下它从“\”中提取cookie。但如果我想从给定的路径获取它,请假设“\ bin \ test”。在这种情况下,我们该怎么做?

提前致谢

1 个答案:

答案 0 :(得分:1)

您需要按路径

滚动自己的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
In a Java Servlet how can I change the value of an existing 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;
}