tomcat版本过底导致的编码问题

解决方案:
//发送端

@WebServlet("/cookieDemo01")
public class cookieDemo01 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取Cookie信息

        System.out.println("7777...");
        Cookie[] cs = request.getCookies();

        //获取数据,遍历Cookies
        if (cs!=null){
            for (Cookie c: cs){
                //对数据进行解码
                String name = URLDecoder.decode(c.getName(),"utf-8");
                String value = URLDecoder.decode(c.getValue(),"utf-8");
                System.out.println(name+":"+value);
            }
        }else {
            System.out.println("获取失败");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

//接受端

@WebServlet("/CookieDemo05")
public class CookieDemo05 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决url编码问题
        //对数据进行编码
        String username = URLEncoder.encode("你好","utf-8");
        String userpwd = URLEncoder.encode("不好","utf-8");

        //1.创建Cookie对象
        Cookie cookie = new Cookie("msg",username+":"+userpwd);

        //设置path.让当前下部署的所有项目共享Cookie信息
        cookie.setPath("/");
        //3.发送Cookie
        response.addCookie(cookie);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值