1.1 會話技術(shù)簡介
??會話可以簡單理解為,一個用戶打開一個瀏覽器,在同一個WEB應(yīng)用上彤枢,點(diǎn)擊多個超鏈接,訪問多個WEB資源筒饰,然后關(guān)閉瀏覽器缴啡,那這整個過程我們稱之為一個會話.
1.1.1 會話技術(shù)作用
會話技術(shù)可以保存用戶在會話過程中所產(chǎn)生的數(shù)據(jù)
會話技術(shù)也可以讓用戶在同一個會話中實(shí)現(xiàn)數(shù)據(jù)的共享
會話應(yīng)用場景的思路風(fēng)暴
-
想保存數(shù)據(jù)到購物車 不重要 個人私有
- 保存哪里 服務(wù)器? 數(shù)據(jù)庫 淘寶 京東 不登錄
- 編程最重要的域?qū)ο? 域?qū)ο?臨時存儲數(shù)據(jù)
-
瀏覽器
- 可能會被清掉 服務(wù)器怎么知道瀏覽器里面有購物車數(shù)據(jù)
- 每次訪問服務(wù)器的時候把這個數(shù)據(jù)給帶過去
-
服務(wù)器
- 占用空間 弄混 我怎么識別這個數(shù)據(jù)是誰的
- 我怎么讓用戶證明這個數(shù)據(jù)是他的
存在瀏覽器,主要問題是帶來帶去 存服務(wù)器主要問題是
我要識別這個數(shù)據(jù)是誰的
歸根到底: 會話技術(shù)就是用來臨時保存數(shù)據(jù)的
-
1.1.12 會話技術(shù)分類
-
cookie
- cookie是一種客戶端技術(shù),程序可以把每一個用戶特有的數(shù)據(jù)瓷们,以響應(yīng)頭set- cookie發(fā)送給每一個用戶的瀏覽器业栅,那最終會以文件的形式保存在用戶的硬盤上,
- 當(dāng)用戶再次使用瀏覽器來訪問我們的WEB服務(wù)器换棚,用戶的瀏覽器會帶上他特有的數(shù)據(jù)式镐,而我們的程序所處理的則是來訪用戶特有的數(shù)據(jù)。
-
session
- session是一種服務(wù)器技術(shù)固蚤,WEB服務(wù)器會在運(yùn)行時為每一個用戶的每一個會話創(chuàng)建一個其獨(dú)享的HttpSession對象娘汞,
- 由于session對象是用戶獨(dú)享的,所以我們可以使用session對象來保存用戶在一個會話過程中所產(chǎn)生的數(shù)據(jù)夕玩。
- session對象也是一個域?qū)ο竽阆遥秶钦麄€會話
-
注意:
- cookie 把數(shù)據(jù)保存在用戶的硬盤上
- session 把數(shù)據(jù)保存在服務(wù)器的內(nèi)存上
-
會話: 打開淘寶--瀏覽--關(guān)閉 一個會話
- cookie
- 存在瀏覽器
- 服務(wù)器先創(chuàng)建出來
- 隨著響應(yīng)帶到瀏覽器,然后瀏覽器做臨時保存,保存完之后,下次請求在帶回去
- 優(yōu)點(diǎn):
- 減少服務(wù)器的壓力,服務(wù)器內(nèi)存可以少用一點(diǎn)
- 缺點(diǎn):
- 換一個瀏覽器就獲取不到
- 隨時都會被清理,適合保存不重要的東西
- 帶來帶去,占用帶寬,適合存小的東西,如果過大,用戶等待的時候很長
- 數(shù)據(jù)傳來傳去, 不安全
- 總結(jié): 不必要的,比較小的,不涉及安全性的內(nèi)容,用cookie
- 存在瀏覽器
- session
- 存在服務(wù)器
- session 就是一個域?qū)ο?(setAttribute getAttribute,removeAttribute),就是通過這個保存數(shù)據(jù)
- cookie
1.2 Cookie 常用方法
1.2.1 Cookie的方法
方法 | 描述 |
---|---|
Cookie(String name, String value) | Constructs a cookie with a specified name and value. |
void setMaxAge(int expiry) | Sets the maximum age of the cookie in seconds. |
void setPath(String uri) | Specifies a path for the cookie to which the client should return the cookie. |
String getName() | Returns the name of the cookie. |
String getValue() | Returns the value of the cookie. |
1.2.2 關(guān)于Cookie對象的方法
Cookie[] cs = request.getCookies();
得到的是一個數(shù)組,通過遍歷可以得到自己想要的Cookie信息
添加Cookie對象
response.addCookie(c);
其中參數(shù)c 為cookie對象
設(shè)置有效時間
void setMaxAge(int expiry)
cookie.setMaxAge(60 * 60 * 24);//有效期一天
cookie.setMaxAge(-1);//當(dāng)瀏覽器關(guān)閉,會話結(jié)束后cookie會自動的被刪除
cookie.setMaxAge(0);//cookie會立刻被刪除
cookie.setMaxAge(Integer.MAX_VALUE);//最長的有效時間
設(shè)置路徑
void setPath(String uri)//霧霾 江蘇 南京市 上海
當(dāng)用戶訪問這個路徑下的web資源,或者是這個路徑下子路徑的資源,他都會自動攜帶這個cookie
cookie.setPath("/myCookie");//cookie的默認(rèn)路徑是當(dāng)前WEB應(yīng)用的根路徑
cookie.setPath(request.getContextPath());//推薦使用這個方法,比較靈活
1.2.3 關(guān)于Cookie的一些細(xì)節(jié)
- 一個cookie只能用來標(biāo)識一種信息燎孟,而cookie至少需要包含這個信息的名稱和值
- 一個網(wǎng)站可以向一個瀏覽器提供多個cookie禽作,而一個瀏覽器也可以接收多個網(wǎng)站所提供的cookie
- 一個瀏覽器一般最多可以接收300個cookie,一個網(wǎng)站所對應(yīng)的cookie最多為20個揩页,一個cookie大小限制為4kb
- 創(chuàng)建一個cookie對象旷偿,發(fā)送至客戶端,那這個cookie他默認(rèn)的級別是會話級別
- 會話級別:當(dāng)用戶關(guān)閉瀏覽器,會話結(jié)束了萍程,那這個cookie也被刪除了
[圖片上傳中...(記錄用戶上次訪問時間分析.png-a00c2d-1522371203795-0)] - 如果需要把cookie保存在用戶的硬盤上幢妄,需要設(shè)置cookie的有效時間(過期時間)
- cookie的名稱和值都不可以使用中文,因?yàn)橹形谋徽J(rèn)為是不安全的字符
1.3 Cookie 入門案例
圖解
記錄用戶上次訪問時間分析.png
1.3.1 記錄用戶上次的訪問時間
思路整理
- 拿到指定的cookie信息
- 判斷是否為null
- 如果為null,給出提示信息
- 否則,獲取上次的訪問時間,并顯示
- 獲得當(dāng)前的時間存在cookie當(dāng)中,發(fā)到客戶端
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//記錄用戶上次的訪問時間
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//獲取cookie信息
Cookie[] cs = request.getCookies();
if(cs != null) {
for (int i = 0; i < cs.length; i++) {
String name = cs[i].getName();
if("lastAccessTime".equals(name)) {
cookie = cs[i];
break;
}
}
}
//判斷是否有上次的訪問時間
if(cookie == null) {
//如果沒有上次的訪問時間茫负,需要給出提示信息
out.println("歡迎第一次訪問");
}
else {
//如果有上次的訪問時間蕉鸳,需要獲取上次的訪問時間并顯示
String value = cookie.getValue();
out.println("您上次的訪問時間為:" + value);
}
//獲取這次的訪問時間(當(dāng)前的系統(tǒng)時間)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String now = sdf.format(new Date());
//創(chuàng)建Cookie對象
Cookie c = new Cookie("lastAccessTime",now);
//記錄用戶這次的訪問時間,并發(fā)送至客戶端
response.addCookie(c);
}
1.3.2 記住用戶名
登錄頁面
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設(shè)置響應(yīng)類型和編碼
response.setContentType("text/html;charset=UTF-8");
//獲取輸出流對象
PrintWriter out = response.getWriter();
Cookie[] cs = request.getCookies();
Cookie cookie = CookieUtils.getCookie(cs, "username");
String username = "";
String checked = "";
if (cookie != null) {
username = cookie.getValue();
checked = "checked = 'checked'";
}
out.println("<html>");
out.println("<head>");
out.println("<title>登錄頁面</title>");
out.println("</head>");
out.println("<body>");
out.println("<form action='/myCookie/LoginServlet' method='post'>");
out.println("用戶名:<input type='text' name='username' value='"+username+"' />");
out.println("<br />");
out.println("密碼:<input type='text' name='password' />");
out.println("<br />");
out.println("<input type='submit' value='登錄' />");
out.println("<input type='checkbox' name='remember' value='1'"+checked+" />記住用戶名");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
跳轉(zhuǎn)頁面
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設(shè)置響應(yīng)類型和編碼
response.setContentType("text/html;charset=UTF-8");
//獲取輸出流對象
PrintWriter out = response.getWriter();
out.println("<h1>我是首頁</h1>");
}
處理頁面
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設(shè)置響應(yīng)類型和編碼
response.setContentType("text/html;charset=UTF-8");
//獲取輸出流對象
PrintWriter out = response.getWriter();
//獲取用戶名
String username = request.getParameter("username");
//獲取密碼
String password = request.getParameter("password");
//獲取復(fù)選框記住用戶名的值
String remember = request.getParameter("remember");
System.out.println(username);
System.out.println(password);
//判斷用戶名和密碼是否有效
if ("itcast".equals(username) && "123456".equals(password)) {
//登錄成功
out.println("登錄成功,3秒后跳轉(zhuǎn)首頁");
response.setHeader("Refresh", "3;URL=/myCookie/IndexServlet");
if ("1".equals(remember)) {
//需要記住用戶名
//創(chuàng)建Cookie對象
Cookie cookie = new Cookie("username", username);
//設(shè)置cookie的有效時間
cookie.setMaxAge(Integer.MAX_VALUE);
//發(fā)送cookie至客戶端
response.addCookie(cookie);
}else {
//不需要記住用戶名
Cookie cookie = new Cookie("username", "");
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}else {
//登錄失敗
out.println("用戶名或者密碼錯誤,3秒后跳轉(zhuǎn)到登錄頁面");
response.setHeader("Refresh", "3;URL=/myCookie/LoginUIServlet");
}
}