我是靠谱客的博主 高大外套,最近开发中收集的这篇文章主要介绍Cookie 的有效路径访问权限,及测试;Cookie的删除演示,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

声明使用的JDK9&Tomcat9版本;

① cookie的有效路径仅仅能被 其自身和其子路径访问;

② 当cookie的有效路径为  /   时候,web项目下所有的路径均可以访问到 cookie;

 测试如下:


创建cookie:

访问 servlet 以生成 cookie path ;

查看 localhost cookie path ,name为 :path ,内容为 : 设置了cookie的 path;

 

cookie的有效路径 /web/a/b/c ,用 servlet2 的路径访问获取 cookie:

 


 

更换 servlet 3的路径访问获取cookie  ,访问不到 cookie: 

访问结果如下: 

 


当有效路径  path 为  /  时候:

 

 然后进行访问 ;

 可以访问到 ;


 

path1 中设置了cookie的 path,path2和path3分别为path的 子路径和父路径; 代码如下:

path1 ;

package com.baidu.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "Path3", urlPatterns = "/web/a")
public class Path3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);//
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //乱码问题
        //获取cookie  然后打印name&value
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName());
            System.out.println(cookie.getValue());
        }
    }
}

path2 ;

package com.baidu.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "Path2", urlPatterns = "/web/a/b/c/d")
public class Path2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);//
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //防止乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取cookie
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {   //打印  cookie 的name&value
            String value = cookie.getValue();
            System.out.println(cookie.getName());
            System.out.println(value);
        }
    }
//    void  setPath(String path)
}

 path3

package com.baidu.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "Path3", urlPatterns = "/web/a")
public class Path3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);//
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //乱码问题
        //获取cookie  然后打印name&value
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName());
            System.out.println(cookie.getValue());
        }
    }
}

然后就可点击启动Tomcat,访问path1服务器进行测试;


 cookie  的删除原理:

使用 创建同名的 cookie 【name相同,value =“ ” 】

并且把 cookie的 存活时间设置为0  ;  新创建的 cookie的有效路径 ,必须要和要删除的 cookie的有效路径保持一致;

这样浏览器会自动替换新的 cookie;

 

 

浏览器中存在name=path  的cookie【就是上面的path1 创建的cookie,现在要删除它】

 创建删除cookie的servlet,并在浏览器中访问该servlet;

查看cookie已经被删除【名字为path的cookie 在localhost 目录下,查看已经不存在该cookie】;

新创建的 cookie 代码如下,要删除的参考上面 path1: 

package com.baidu.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "DeleteCookie", urlPatterns = "/del")
public class DeleteCookie extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);//
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //利用cookie 的同名覆盖delete cookie
        Cookie cookie = new Cookie("path","");
        cookie.setMaxAge(0);
        cookie.setPath("/web/a/b/c");//要保证有效路径一致
        response.addCookie(cookie);
        response.getWriter().print("删除成功");
    }
}

 

最后

以上就是高大外套为你收集整理的Cookie 的有效路径访问权限,及测试;Cookie的删除演示的全部内容,希望文章能够帮你解决Cookie 的有效路径访问权限,及测试;Cookie的删除演示所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部