本帖最后由 大山哥哥 于 2017-6-30 10:46 编辑
在JaveEE课程中,我们Cookie的API学习主要集中在name、value、maxAge的使用,针对path的讲解比较少,下面,将对Cookie中的path进行简单的阐述。
public void setPath(String uri)
指定客户端应该返回 cookie 的路径。 cookie 对于指定目录中的所有页面及该目录子目录中的所有页面都是可见的。cookie 的路径必须包括设置 cookie 的 servlet,例如 /catalog,它使 cookie 对于服务器上 /catalog 下的所有目录都是可见的.
举例如下:
[XML] 纯文本查看 复制代码 <!-- 准备三个servlet。
CookiePath1是设置path的值
CookiePath2是获取path的值
CookiePath3是获取path的值
-->
<servlet>
<servlet-name>CookiePath1</servlet-name>
<servlet-class>com.itheima.cookie.CookiePath1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CookiePath1</servlet-name>
<url-pattern>/path1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>CookiePath2</servlet-name>
<servlet-class>com.itheima.cookie.CookiePath2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CookiePath2</servlet-name>
<url-pattern>/path2</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>CookiePath3</servlet-name>
<servlet-class>com.itheima.cookie.CookiePath3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CookiePath3</servlet-name>
<url-pattern>/path3</url-pattern>
</servlet-mapping>
[Java] 纯文本查看 复制代码 /**
* CookiePath1作用:new一个cookie并回写到浏览器
* 使用最简单的servlet设置cookie的path为/urlPattern/path2
*/
public class CookiePath1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie = new Cookie("demo1", "value1");
//设置path为"/urlPattern"
cookie.setPath("/urlPattern/path2");
//将cookie回写到 浏览器
response.addCookie(cookie);
response.getWriter().write("cookie return OK");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
通过访问path1(http://localhost:8080/urlPattern/path1),将cookie回写到浏览器,响应如下:
[Java] 纯文本查看 复制代码 /**
* url-pattern为 /path2
* 获取cookie值
*/
public class CookiePath2 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//根据cookie名称获取cookie
Cookie cookie = CookieUtil.getNamedCookie(request.getCookies(),"demo1");
if(cookie != null){
String value = cookie.getValue();
System.out.println("得到cookie(demo1)的值是:" + value);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
访问path2路径(localhost:8080/urlPattern/path2),尝试获取cookie信息。控制台输出如下:
[Java] 纯文本查看 复制代码 /**
* url-pattern为 /path3
* 获取cookie值
*/
public class CookiePath3 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//根据cookie名称获取cookie
Cookie cookie = CookieUtil.getNamedCookie(request.getCookies(),"demo1");
if(cookie != null){
String value = cookie.getValue();
System.out.println("得到cookie(demo1)的值是:" + value);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
访问path3路径(localhost:8080/urlPattern/path3),尝试获取cookie信息。控制台无打印。
说明,path3无法获取到名称为demo1的cookie值。
回到API中的说明,只有符合该cookie设置的path或者子目录,才可以访问。
我们将path3的url-pattern拦截路径更改成
[XML] 纯文本查看 复制代码 <url-pattern>/path2/path3</url-pattern>
之后,在进行path3访问(localhost:8080/url-pattern/path2/path3),控制台可以打印cookie数据,并且在请求中也可以看到该cookie
当new一个Cookie回写到浏览器,但是并且没有设置path时,有如下的path默认值。
(1) 当URI的path值是以“/”结尾的时候,直接设置为cookie的path值
(2) 当URI的path值不是以“/”结尾的时候,查看path里面是否有“/”
如果有“/”的话,直接截取到最后一个“/”,然后设置为cookie的path值。
举例说明:
[Java] 纯文本查看 复制代码 Cookie cookie = new Cookie("demo1", "value1");
//将cookie回写到 浏览器
response.addCookie(cookie);
response.getWriter().write("cookie return OK");
那么,响应的cookie截图如下:
如果没有“/”的话,将cookie的path设置为”/”。(比如访问路径是localhost:8080),那么cookie的默认path值是“/”
|