2013-08-02 66 views
0

我們如何從給定的路徑獲取曲奇?當我們做request.getCookies()時,它默認從「\」中提取cookie。但如果我想從給定的路徑中獲取它,則假定「\ bin \ test」。在這種情況下,我們應該怎麼做?如何從JAVA中的給定路徑獲取cookie?

在此先感謝

+0

1日的問題是,你能夠獲得請求標題的東西?即使「\ bin \ test」的cookie值被髮送到您的jsp文件? – Jianhong

回答

0

您將需要通過路徑推出自己的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; 
}