一口一个菠萝 2018-10-28 07:30 采纳率: 50%
浏览 829
已采纳

servlet清空cookie时遇到的很奇怪的问题,求教

我自己写的登出代码:

 public void logout(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        // 从session中将user删除
        session.removeAttribute("user");

        // 将存储在客户端的cookie删除掉
        Cookie cookie_username = new Cookie("cookie_username", null);
        cookie_username.setMaxAge(0);
        cookie_username.setPath(request.getContextPath());
        // 创建存储密码的cookie
        Cookie cookie_password = new Cookie("cookie_password", null);
        cookie_password.setMaxAge(0);
        cookie_password.setPath(request.getContextPath());
        response.addCookie(cookie_username);
        response.addCookie(cookie_password);

        try {
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

当我把 cookie_username.setPath设置为"/"的时候,登出就没有效果了,有没有大神能告诉我是为什么吗

  • 写回答

1条回答 默认 最新

  • devmiao 2018-10-28 17:06
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失