// 当点击 的时候跳转到 session2
response.setContentType("text/html;charset=utf-8");
//此方法会在路径后面自动拼接sessionId
String path = response.encodeURL("/day11/session2");
System.out.println(path);
//页面输出
response.getWriter().println("ip地址保存成功,想看 请<a href='" + path + "'>点击</a>");
// 重定向到session2
String path = response.encodeRedirectURL("/day11/session2");
System.out.println("重定向编码后的路径:" + path);
response.sendRedirect(path);
session2代码,获得session1传过来的ID
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 需求:从session容器中取出ip
// 获得session对象
HttpSession session = request.getSession();
// 获取ip地址
String ip = (String) session.getAttribute("ip");
// 将ip打印到浏览器中
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("IP:" + ip);
}
session1代码
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 需求:将ip保存到session中
// 获取session
HttpSession session = request.getSession();
// 获得ip
String ip = request.getRemoteAddr();
// 将ip保存到session中
session.setAttribute("ip", ip);
// 需求2:手动的将 session对应的cookie持久化,关闭浏览器再次访问session中的数据依然存在
// 创建cookie
Cookie cookie = new Cookie("JSESSIONID", session.getId());
// 设置cookie的最大生存时间
cookie.setMaxAge(60 * 30);
// 设置有效路径
cookie.setPath("/");
// 发送cookie
response.addCookie(cookie);
// 当点击 的时候跳转到 session2
// response.setContentType("text/html;charset=utf-8");
// String path = response.encodeURL("/day11/session2");
// System.out.println(path);
// response.getWriter().println("ip地址保存成功,想看 请<a href='" + path + "'>点击</a>");
// 重定向到session2
String path = response.encodeRedirectURL("/day11/session2");
System.out.println("重定向编码后的路径:" + path);
response.sendRedirect(path);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有