标签:
用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
标签:
原文地址:http://www.cnblogs.com/daxiong225/p/4774196.html