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

  • 相关阅读:
    AMH4.2 Ftp账号路径修改设置
    过狗一句话
    破解tumblr背景音乐
    lnmp下安装ffmpeg和ffmpeg-php教程
    How To install FFMPEG, FLVTOOL2, MP4Box on CentOS server 2015 easy method
    自己的路删除
    弹出CPA
    JSON的相关知识
    JavaScript函数的相关知识
    JavaScript对象的相关知识
  • 原文地址:https://www.cnblogs.com/daxiong225/p/4774196.html
Copyright © 2011-2022 走看看