码迷,mamicode.com
首页 > 其他好文 > 详细

Session会话跟踪

时间:2015-08-31 23:21:43      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

用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:\t"+session.isNew()+"<br>");
out.println("sessionID\t:"+session.getId()+"<br>");
session.setAttribute("addr", "上海市。黄浦区");
String formUrl = "./sessionB";
formUrl = resp.encodeURL(formUrl);
out.print("<center>"
+ "<form action=\""+formUrl+"\">\n"
+"<input type=\"text\" name=\"name\" value=\"momo\">\n"+
"<input type=\"SUBMIT\" value=\"submit\">\n"+
"</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:\t"+session.isNew()+"<br>");
out.println("sessionID\t:"+session.getId()+"<br>");
String name = req.getParameter("name");
out.println("addr:\t"+addr+"<br>");
out.println("name:\t"+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

 

Session会话跟踪

标签:

原文地址:http://www.cnblogs.com/daxiong225/p/4774196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!