zoukankan      html  css  js  c++  java
  • 在禁用cookie时操作Session

    介绍

    当客户端禁用了cookie时,造成session无法访问,此时我们可以使用URL重写来解决这个问题

    URL重写要求将站点中的所有超链接都进行改造,在超链接后用一个特殊的参数JSESSIONID保存当前浏览器对应session的编号,这样一来,当用户点击超链接访问服务器时,服务器可以从URL后的参数中分析出JSESSIONID,从而找到对应的sesison使用.

    URL重写之前,要先创建出session,才能进行重写操作

    --------------------------------

    response.encodeURL(String url);

    //--如果是普通的地址用这个方法

    response.encodeRedirectURL(String url);

    //--如果地址是用来进行重定向的,用这个方法

    ---------------------------------

    以上两个方法可以实现URL重写,这两个方法非常的智能,只要发现浏览器发送了任何Cookie过来,就认为当前客户端没有禁用Cookie,就不会在进行URL重写

    ---------------------------------

    举例:

    重写URL

    jsp页面

     1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" session="false"%>
     2 <!DOCTYPE HTML>
     3 <html>
     4     <head>
     5         <title>My JSP 'index.jsp' starting page</title>
     6         <meta http-equiv="pragma" content="no-cache">
     7         <meta http-equiv="cache-control" content="no-cache">
     8         <meta http-equiv="expires" content="0">
     9     </head>
    10     <body>
    11         <h1>商品列表</h1>
    12         <%
    13         //在url后拼接上session的id 防止在禁用cookie时候影响session的使用
    14             HttpSession session = request.getSession();
    15             String url1=response.encodeURL(request.getContextPath()+"/servlet/BuyServlet?prod=小米手机");
    16             String url2=response.encodeURL(request.getContextPath()+"/servlet/BuyServlet?prod=海尔洗衣机");
    17             String url4=response.encodeURL(request.getContextPath()+"/servlet/PayServlet");
    18         
    19         
    20         
    21          %>
    22         <a href="<%= url1 %>">小米手机</a><br/><br/>
    23         <a href="<%= url2 %>">海尔洗衣机</a><br/><br/>
    24         <a href="<%= request.getContextPath() %>/servlet/BuyServlet?prod=阿迪王">阿迪王皮鞋</a><br/><br/>
    25         <a href="<%= url4 %>">支付</a><br/><br/>
    26     </body>
    27 </html>

     

    存入Session

    servlet

     1 package cn.tedu.session;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.ServletException;
     6 import javax.servlet.http.Cookie;
     7 import javax.servlet.http.HttpServlet;
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 import javax.servlet.http.HttpSession;
    11 /**
    12  * 案例:购物车
    13  */
    14 public class BuyServlet extends HttpServlet {
    15 
    16     public void doGet(HttpServletRequest request,
    17             HttpServletResponse response)
    18             throws ServletException, IOException {
    19         response.setContentType("text/html;charset=utf-8");
    20         
    21         //获取商品信息 控制字符集影响
    22         String prod = request.getParameter("prod");
    23         byte[] bytes = prod.getBytes("iso8859-1");
    24         prod = new String(bytes, "utf-8");
    25         
    26         //获取session, 保存数据到session中
    27         HttpSession session = request.getSession();
    28         session.setAttribute("prod", prod);
    29         
    30         //创建一个Cookie, 这个Cookie的名字和path要和服务器创建的Cookie一样
    31         Cookie cookie = new Cookie("JSESSIONID", session.getId());
    32         cookie.setPath(request.getContextPath()+"/");
    33         
    34         cookie.setMaxAge(3600);//设置最大生存时间为1小时
    35         
    36         response.addCookie(cookie);
    37         
    38         response.getWriter().write("成功将["+prod+"]商品加入了购物车...");
    39     }
    40 
    41     public void doPost(HttpServletRequest request,
    42             HttpServletResponse response)
    43             throws ServletException, IOException {
    44         doGet(request, response);
    45     }
    46 
    47 }

     

    读取session

    servlet 

     1 package cn.tedu.session;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.ServletException;
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 import javax.servlet.http.HttpSession;
    10 /**
    11  * 案例:购物车
    12  */
    13 public class PayServlet extends HttpServlet {
    14 
    15     public void doGet(HttpServletRequest request,
    16             HttpServletResponse response)
    17             throws ServletException, IOException {
    18         response.setContentType("text/html;charset=utf-8");
    19         
    20         //获取为当前客户端服务的session, 从中取出商品信息
    21         HttpSession session = request.getSession(false);
    22         
    23         if(session != null){
    24             String prod = (String) session.getAttribute("prod");
    25             response.getWriter().write("成功为["+prod+"]商品支付10000$.....");
    26         }else{
    27             response.getWriter().write("您还没有将任何商品加入购物车.....");
    28         }
    29     }
    30 
    31     public void doPost(HttpServletRequest request,
    32             HttpServletResponse response)
    33             throws ServletException, IOException {
    34         doGet(request, response);
    35     }
    36 
    37 }

     

  • 相关阅读:
    MySQL mysqldump数据导出详解
    FTP上传下载 C#辅助类
    FastDFS java 辅助类
    Ajax 提交表单【包括文件上传】
    bootstrap-table 基础用法
    MVC dropdownlist 后端设置select属性后前端依然不能默认选中的解决方法
    jQuery实现鼠标移到元素上动态提示消息框效果
    给Jquery动态添加的元素添加事件
    centos7部署mysql5.7一主多从
    iOS浏览器 new Date() 返回 NaN
  • 原文地址:https://www.cnblogs.com/pxffly/p/7488139.html
Copyright © 2011-2022 走看看