Cookie 的 作用范圍:
Cookie詳解:https://www.cnblogs.com/handsomecui/p/6117149.html
- 可以作用當前目錄和當前目錄的子目錄. 但不能作用於當前目錄的上一級目錄.
- void setPath(java.lang.String uri) :設置cookie的有效訪問路徑。有效路徑指的是cookie的有效路徑保存在哪里,那么瀏覽器在有效路徑下訪問服務器時就會帶着cookie信息,否則不帶cookie信息。
<% Cookie cookie = new Cookie("cookiePath", "CookiePathValue"); //設置 Cookie 的作用范圍: String contextPath = request.getContextPath();
//如果web項目沒有指定 Application context 給它一個默認值“/”,否則設置 Cookie 的作用范圍無效 if(contextPath.trim().equals("")){ contextPath = "/"; } cookie.setPath(contextPath); response.addCookie(cookie);
%>