会话技术_Session_原理分析与会话技术_Session_细节
会话技术_Session_原理分析
Session_原理分析:
Session的实现是依赖于Cookie的
它们是通过请求和响应头完成的
请求头中有Cookie,在其中有JSESSIONID
SessionDemo1响应头:JSESSIONID=A78B24B30DA471BFC4ABC8CD479B677F;
SessionDemo2请求头:JSESSIONID=A78B24B30DA471BFC4ABC8CD479B677F;
会话技术_Session_细节
Session_细节问题:
1.当客户端关闭后,服务器不关闭,两次获取Session是否为同一个?
默认情况下。不是。
如果需要相同,则可以创建Cookie,键为JSESSIONID,设置最大存活时间,让cookie持久化保存
package bd.xueqiang.Session; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; @WebServlet("/sessionDemo3") public class SessionDemo3 extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取session HttpSession session = request.getSession(); System.out.println(session); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
package bd.xueqiang.Session; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; @WebServlet("/sessionDemo3") public class SessionDemo3 extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取session HttpSession session = request.getSession(); System.out.println(session); //期望客户端关闭后,Session也能相同 Cookie cookie = new Cookie("JSESSIONID",session.getId()); cookie.setMaxAge(60 * 60); response.addCookie(cookie); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
2.客户端不关闭,服务器后,两次获取Session是同一个吗?
3.Session的失效时间?