java中Cookie被禁用后Session追踪问题

一.服务器端获取Session对象依赖于客户端携带的Cookie中的JSESSIONID数据。如果用户把浏览器的隐私级别调到最高,这时浏览器是不会接受Cookie、这样导致永远在服务器端都拿不到的JSESSIONID信息。这样就导致服务器端的Session使用不了。 

Java针对Cookie禁用,给出了解决方案,依然可以保证JSESSIONID的传输。

Java中给出了再所有的路径的后面拼接JSESSIONID信息。

在 Session1Servlet中,使用response.encodeURL(url) 对超链接路径拼接 session的唯一标识

1
2
3
4
5
6
7
// 当点击 的时候跳转到 session2
   response.setContentType("text/html;charset=utf-8");
   //此方法会在路径后面自动拼接sessionId
   String path = response.encodeURL("/day11/session2");
   System.out.println(path);
   //页面输出
   response.getWriter().println("ip地址保存成功,想看 请<a href='" + path + "'>点击</a>");

二.在response对象中的提供的encodeURL方法它只能对页面上的超链接或者是form表单中的action中的路径进行重写(拼接JSESSIONID)。 

如果我们使用的重定向技术,这时必须使用下面方法完成:其实就是在路径后面拼接了 Session的唯一标识 JSESSIONID。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// 重定向到session2
    String path = response.encodeRedirectURL("/day11/session2");
    System.out.println("重定向编码后的路径:" + path);
    response.sendRedirect(path);
session2代码,获得session1传过来的ID
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 需求:从session容器中取出ip
    // 获得session对象
    HttpSession session = request.getSession();
    // 获取ip地址
    String ip = (String) session.getAttribute("ip");
    // 将ip打印到浏览器中
    response.setContentType("text/html;charset=utf-8");
    response.getWriter().println("IP:" + ip);
  }
session1代码
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 需求:将ip保存到session中
    // 获取session
    HttpSession session = request.getSession();
    // 获得ip
    String ip = request.getRemoteAddr();
    // 将ip保存到session中
    session.setAttribute("ip", ip);
    // 需求2:手动的将 session对应的cookie持久化,关闭浏览器再次访问session中的数据依然存在
    // 创建cookie
    Cookie cookie = new Cookie("JSESSIONID", session.getId());
    // 设置cookie的最大生存时间
    cookie.setMaxAge(60 * 30);
    // 设置有效路径
    cookie.setPath("/");
    // 发送cookie
    response.addCookie(cookie);
    // 当点击 的时候跳转到 session2
    // response.setContentType("text/html;charset=utf-8");
    // String path = response.encodeURL("/day11/session2");
    // System.out.println(path);
    // response.getWriter().println("ip地址保存成功,想看 请<a href='" + path + "'>点击</a>");
    // 重定向到session2
    String path = response.encodeRedirectURL("/day11/session2");
    System.out.println("重定向编码后的路径:" + path);
    response.sendRedirect(path);
  }

 

 

 
Dubbo项目高级篇 Dubbo分布式系统架构视频教程-ZooKeeper-第三方支付项目精华课程 attach_img
Dubbo项目基础篇 Dubbo分布式系统架构视频教程-ZooKeeper-第三方支付项目精华课程 attach_img
JAVAEE SSH lucene POI 国家电力项目附源码+文档 10天 attach_img
潭州学院大型企业内部技术 java课程 attach_img
SpringMVC微服务架构实战之基于事务的分布式解决方案高清视频附源码讲义 31课 attach_img
JAVA数据结构算法视频教程 attach_img
Java8系列教程:Java8编程入门、面向对象编程、高级编程、核心设计模式 DAO设计模式 attachment
JavaEE开发的颠覆者 Spring Boot实战 pdf完整版
SSM整合视频教程
微信开发Java版3套
2017Java工程师实战SpringBoot底层原理  ...2
JavaEE开发的颠覆者 Spring Boot实战 pdf
Spring Boot学习入门+进阶+微服务等视频资源集合[英文视频教程] attach_img
微服务必学SpringBoot高清视频教程附源码讲义 17课  ...2
【大数据基础】炼数成金大数据的Java基础 14课
高级java软件架构师实战培训视频教程
NIO+Netty5各种RPC架构实战演练
深入浅出Netty源码剖析 attach_img
Ant视频教程
web层最火框架——尚硅谷SpringMVC视频教程
posted @ 2018-11-25 19:37  winifredaf  阅读(1934)  评论(0编辑  收藏  举报