Session&Cookie

Session&Cookie

會話
  • 一次會話中存在多次請求和響應
  • 瀏覽器第一次給服務器發送資源,回話建立,可以訪問多次資源,服務器或者瀏覽器關閉,會話結束。
  • 目前存在兩種會話技術
    • 客戶端會話技術 cocokie
    • 服務器端會話技術 Session
Cookie
簡介
  • 將數據保存在客戶端
  • 客戶端再次請求服務器的時候攜帶上信息
原理
  • 基於響應頭set-cookie和請求頭cookie實現

在這裏插入圖片描述

Cookie[] cookies = req.getCookies(); //獲得Cookie
cookie.getName(); //獲得cookie中的key
cookie.getValue(); //獲得cookie中的vlaue
new Cookie("lastLoginTime", System.currentTimeMillis()+""); //新建一個cookie
cookie.setMaxAge(24*60*60); //設置cookie的有效期
resp.addCookie(cookie); //響應給客戶端一個cookie

cookie:一般會保存在本地的 用戶目錄下 appdata;

一個網站cookie是否存在上限!

  • 一個Cookie只能保存一個信息;
  • 一個web站點可以給瀏覽器發送多個cookie,最多存放20個cookie;
  • Cookie大小有限制4kb;
  • 300個cookie瀏覽器上限

刪除Cookie;

  • 不設置有效期,關閉瀏覽器,自動失效;
  • 設置有效期時間爲 0 ;setMaxAge
Session
簡單使用
  • 在多次請求之間共享數據,數據保持在服務器端。服務器端有一個Session對象專門用來保存信息。

  • 實驗 demo01放置信息到Session中 demo02從Session中獲取到信息

    • demo01
    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;
    
    /**
     * @author ECHOQIAN
     * @version 1.0
     * @date 2019/9/17 15:29
     */
    public class SessionDemo01 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html");
    
    //      獲取session 設置屬性值
            System.out.println("into 01");
            HttpSession session = req.getSession();
            session.setAttribute("name", "echoqian");
    //      得到sessionId
            String sessionId = session.getId();
    
            //判斷Session是不是新創建
            if (session.isNew()){
                resp.getWriter().write("session創建成功,ID:"+sessionId);
            }else {
                resp.getWriter().write("session以及在服務器中存在了,ID:"+sessionId);
            }
        }
    }
    
    
    • demo02
    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;
    
    /**
     * @author ECHOQIAN
     * @version 1.0
     * @date 2019/9/17 15:33
     */
    public class SessionDemo02 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //獲取session 設置屬性值
            HttpSession session = req.getSession();
    
            String msg = (String) session.getAttribute("name");
            resp.getWriter().write("get msg from session: " + msg);
        }
    }
    
Session原理(基於cookie實現)

在這裏插入圖片描述

會話失效
  • 可以在web.xml裏面設置

    • <!--設置Session默認的失效時間-->
      <session-config>
          <!--15分鐘後Session自動失效,以分鐘爲單位-->
          <session-timeout>15</session-timeout>
      </session-config>
      
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章