zoukankan      html  css  js  c++  java
  • Session会话跟踪

    用encodeURL重写URL 

    public class SessionA extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    resp.setContentType("text/html");
    resp.setCharacterEncoding("utf-8");
    PrintWriter out = resp.getWriter();
    HttpSession session = req.getSession();
    out.println("seesion is new: "+session.isNew()+"<br>");
    out.println("sessionID :"+session.getId()+"<br>");
    session.setAttribute("addr", "上海市。黄浦区");
    String formUrl = "./sessionB";
    formUrl = resp.encodeURL(formUrl);
    out.print("<center>"
    + "<form action=""+formUrl+""> "
    +"<input type="text" name="name" value="momo"> "+
    "<input type="SUBMIT" value="submit"> "+
    "</form>"
    + "</center>"
    );
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(req, resp);
    }

    }

    SessionB:

    public class SessionB extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    resp.setContentType("text/html");
    resp.setCharacterEncoding("utf-8");
    PrintWriter out = resp.getWriter();
    HttpSession session = req.getSession();
    String addr = (String) session.getAttribute("addr");
    out.println("seesion is new: "+session.isNew()+"<br>");
    out.println("sessionID :"+session.getId()+"<br>");
    String name = req.getParameter("name");
    out.println("addr: "+addr+"<br>");
    out.println("name: "+name+"<br>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(req, resp);
    }

    }

    首次访问

    sessionA:http://localhost:8080/cookieAndsession/sessionA

    结果:

    点击submit提交到sessionB

    URL重写为:http://localhost:8080/cookieAndsession/sessionB;jsessionid=5FA288B80EA17513084ABD6D202E12DF?name=momo

    通过encodeURL重写url会把会话的id和参数添加到URL后面jsessionid=5FA288B80EA17513084ABD6D202E12DF?name=momo

    结果:

    seesion is new: false
    sessionID :5FA288B80EA17513084ABD6D202E12DF
    addr: 上海市。黄浦区
    name: momo

  • 相关阅读:
    ASP.NET MVC5 生成验证码
    System.Web.Caching.Cache类 Asp.Net缓存 各种缓存依赖
    ASP.NET访问网络驱动器(映射磁盘)
    ASP.NET Cache 实现依赖Oracle的缓存策略
    目前主流的四大浏览器内核Trident、Gecko、WebKit以及Presto
    ASP.NET 5 改名 ASP.NET Core 1.0
    C# 7.0新加特性
    elasticsearch 单实例安装启动
    关于mybatis 一级缓存引发的问题
    关于springboot 打包问题 jar包和 war包
  • 原文地址:https://www.cnblogs.com/daxiong225/p/4774196.html
Copyright © 2011-2022 走看看