Cookie、Session
1 會話
會話: 用戶打開一個瀏覽器,點擊了很多超鏈接,訪問多個web資源,關閉瀏覽器,這個過程叫做會話。
有狀態會話: 一個同學來過教室,下次再來教室的時候,我們都知道他曾經來過,這個過程稱之爲有狀態會話。
一個網站,怎麼證明你來過?
客戶端 服務端
- 服務端給客戶端一個信件,客戶端下次訪問服務器時帶上信件就可以了;cookie
- 服務器登記你來過了,下次你來的時候我來匹配你;session
2 保存會話的兩種技術
cookie
- 客戶端技術(響應,請求)
session
- 服務器技術,利用這個技術,可以保存用戶的會話信息,我們可以把信息或者數據放在session中!
常見場景:網站登錄之後,第二次不用再登陸!
3 Cookie
-
從請求中拿到cookie信息
-
服務器響應給客戶端cookie
Cookie[] cookies = req.getCookies();//獲得Cookie cookie.getName(); //獲得cookie中的key cookie.getValue(); //獲得cookie中的value Cookie cookie = new Cookie("LastLoginTime", System.currentTimeMil+""); //新建一個cookie cookie.setMaxAge(24*60*60); //設置cookie的有效期 resp.addCookie(cookie); //響應給客戶端一個cookie
Cookie: 一般會保存在本地的用戶目錄下appdata;
cookie圖示:
cookie補充:
一個網站cookie是存在上限的:
- 一個Cookie只能保存一個信息;
- 一個web站點可以給瀏覽器發送多個cookie,最多存放20個cookie;
- Cookie大小有限制4kb;
- 300個cookie瀏覽器上限;
刪除Cookie:
- 不設置有效期,關閉瀏覽器,自動失效;
- 設置有效期爲0
編碼解碼:
//2-3行爲一般編碼時均需寫上的代碼!保證中文的正常傳輸!
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
Cookie cookie = new Cookie("name", URLEncoder.encode("佳少","utf-8"));//編碼
out.write(URLDecoder.decode(cookie.getValue(),"utf-8"));//解碼
4 Session(重點)
什麼是Session:
- 服務器會給每一個用戶(瀏覽器)創建一個Session對象
- 一個Session獨佔一個瀏覽器,只要瀏覽器沒有關閉,這個Session就存在;
- 用戶登錄之後,整個網站都可以訪問! --> 保存用戶的信息,保存購物車的信息;
Session圖示:
Session和Cookie的區別:
- Cookie是把用戶的數據寫給用戶的瀏覽器,瀏覽器保存;
- Session把用戶的數據寫到用戶獨佔的Session中,服務器端保存(保存重要的信息,減少服務器資源的浪費);
- Session對象由服務器創建;
使用場景:
- 保存一個登錄用戶的信息;
- 購物車信息;
- 在整個網站中經常會使用的數據,將其保存在Session中;
使用Session代碼:
package com.demut.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解決亂碼問題
resp.setCharacterEncoding("utf-8");
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//得到Session
HttpSession session = req.getSession();
//給Session中存東西
session.setAttribute("name","Demut");
session.setAttribute("name",new Person("Demut",1));
//獲取Session的ID
String id = session.getId();
//判斷Session是否爲新創建的
if (session.isNew()){
resp.getWriter().write("session創建成功! ID: "+ id);
}else {
resp.getWriter().write("session已經存在! ID: "+ id);
}
// //Session創建的時候做了什麼事情?
// Cookie cookie = new Cookie("JSESSIONID",id);
// resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
//獲取Session中的東西
Person person = (Person) session.getAttribute("name");
resp.getWriter().write(person.toString());
//註銷session
session.removeAttribute("name");
//手動註銷session
session.invalidate();
注意:session失效的同時,瀏覽器會立即創建一個新的session
會話自動過期,web.xml中配置:
<!--設置session默認的失效時間-->
<session-config>
<!--一天後Session自動失效,以分鐘爲單位-->
<session-timeout>1440</session-timeout>
</session-config>
寫在最後
Turning your ear to wisdom and applying your heart to understanding!
側耳聽智慧,專心求聰明!
To Demut and Dottie!