原理
我们都知道,浏览器无状态的。浏览器是操作不了session的,浏览器能够做的只是传递cookie,每次都传递。 把当前主机下的,和当前请求相同域下的cookie 传递到服务器去,只要cookie没过时。
当我们第一次访问某个web 应用的时候,比如 http://localhost:8080/, 且假设之前没有访问过,那么 当前浏览器是不会存在 http://localhost:8080/ 网站的 session cookie 的,那么, 我们的请求头就不会包含 类似于: Cookie:JSESSIONID=C0EC2D80C7DE37C26013EF3FB4ED8B2E 这样的请求头cookie。 然后, 必然的,web 服务器会返回来一个类似这样的请求头:
Set-Cookie:JSESSIONID=9BAE417668B911F21FBBC2BD909B36A8; Path=/; HttpOnly
浏览器会识别Set-Cookie, 然后读取其内容,然后添加到浏览器 对应域名下 cookie池中去。 之后,每次都会携带这个JSESSIONID的cookie。 直到服务器端对当前session进行了invalidate 操作,或者 当前session 超时,那么, 它就失效了(注意session 失效了,但是 当前域下面的其他 cookie 却不会失效的,可以说失效的cookie 仅仅是JSESSIONID )。 然后,我们再次发送http请求, web 服务器又会给我们分配新的 session, 也就是再次响应Set-Cookie:xxx 。
做个测试。
提供两个servlet, 第一个给session属性,
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; /** * Created by luokai on 2017/10/15. */ public class SessionOperationServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); session.setAttribute("user", "lk"); System.out.println("new session isNew = " + session.isNew()); System.out.println("new session getMaxInactiveInterval = " + session.getMaxInactiveInterval()); // session 是不需要login 的,但是可以被 设置为失效。 } }
第二个让session失效:
import javax.servlet.ServletException; import javax.servlet.http.*; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; /** * Created by luokai on 2017/10/15. * * 这里的Logout ,其实就是 invalidate 操作 * */ public class SessionLogoutServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); // request.getSession(true); 这样会新建 session System.out.println("session has attr = " + session.getAttributeNames().hasMoreElements()); if (session.getAttributeNames().hasMoreElements()) { dumpSession(session); } System.out.println("session id = " + session.getId()); session.invalidate(); session = request.getSession(); System.out.println("new session isNew = " + session.isNew());// invalidate后的第一次request.getSession(), 那么session.isNew() 为true System.out.println("new session getMaxInactiveInterval = " + session.getMaxInactiveInterval()); System.out.println("new session has attr = " + session.getAttributeNames().hasMoreElements()); System.out.println("new session id = " + session.getId()); } private static void dumpSession(HttpSession session) { Enumeration<String> attributeNames = session.getAttributeNames(); while (attributeNames.hasMoreElements()) { String s = attributeNames.nextElement(); System.out.println(s + " = " + session.getAttribute(s)); } } }
web.xml配置:
<servlet> <servlet-name>sessOut</servlet-name> <servlet-class>SessionLogoutServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>sessOut</servlet-name> <url-pattern>/sessOut</url-pattern> </servlet-mapping> <servlet> <servlet-name>sessOper</servlet-name> <servlet-class>SessionOperationServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>sessOper</servlet-name> <url-pattern>/sessOper</url-pattern> </servlet-mapping>
先访问 http://localhost:8080/sessOper
然后访问 http://localhost:8080/sessOut
, 我们就可以看到 session是如何起作用了的!