第一章-會話
1.會話的概念
?用戶打開瀏覽器,瀏覽不同的網頁(資源),發出多個請求,直到關閉瀏覽器的程序,稱為一次會話(多次請求). 如同打電話.
?我們在會話的程序(多次請求)之中,用戶可能會產生一些資料,這些資料話有的需要保存起來的,我們就可以通過會話技術來保存用戶各自的資料
2.為什么要使用會話技術
? 保存用戶各自(以瀏覽器為單位)的資料,
3.常用的會話技術
3.1.1cookie
?cookie是客戶端(瀏覽器)端的技術,用戶瀏覽的資訊以鍵值對(key=value)的形式保存在瀏覽器上,如果沒有關閉瀏覽器,再次訪問服務器,會把cookie帶到服務端,服務端就可以做回應的處理,
3.1.2session
?session是服務器端的技術,服務器為每一個瀏覽器開辟一塊記憶體空間,即session,由于記憶體空間是每一個瀏覽器獨享的,所有用戶在訪問的時候,可以把資訊保存在session物件中,同時,每一個session物件都對應一個sessionId,服務器把sessionId寫到cookie中,再次訪問的時候,瀏覽器會把cookie(sessionId)帶過來,找到對應的session物件,
第二章-Cookie
2.1 Cookie的概念
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-SkH3T38J-1619450555084)(img/tu_2.png)]](https://img.uj5u.com/2021/04/29/240311291100131.png)
?Cookie是一種客戶端的會話技術,它是服務器存放在瀏覽器的一小份資料,瀏覽器以后每次訪問該服務器的時候都會將這小份資料攜帶到服務器去,
2.2 Cookie的作用
- 在瀏覽器中存放資料
- 將瀏覽器中存放的資料攜帶到服務器
2.3 Cookie的應用場景
1.記住用戶名
當我們在用戶名的輸入框中輸入完用戶名后,瀏覽器記錄用戶名,下一次再訪問登錄頁面時,用戶名自動填充到用戶名的輸入框.
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-yPYeLNs8-1619450555088)(img/2.png)]](https://img.uj5u.com/2021/04/29/240311291100132.png)
2.自動登錄(記住用戶名和密碼)
當用戶在淘寶網站登錄成功后,瀏覽器會記錄登錄成功的用戶名和密碼,下次再訪問該網站時,自動完成登錄功能.
以上這些場景都是使用會話cookie實作的,將上次的資訊保存到了cookie中,下次直接從cookie中獲取資料資訊
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-cMzkA8Zm-1619450555092)(img/3.png)]](https://img.uj5u.com/2021/04/29/240311291100133.png)
3.保存網站的上次訪問時間
我們訪問網站的時候,通常會看到網站上顯示上次訪問時間,這些資訊就是在用戶訪問網站的時候保存在cookie中的
4.保存電影的==播放進度 ==
? 在網頁上播放電影的時候,如果中途退出瀏覽器了,下載再打開瀏覽器播放同一部電影的時候,會自動跳轉到上次退出時候的進度,因為在播放的時候會將播放進度保存到cookie中
Cookie的快速入門
相關的API
- 創建一個Cookie物件(cookie只能保存字串資料,且不能保存中文)
new Cookie(String name,String value);
- 把cookie寫回瀏覽器
response.addCookie(cookie);
- 獲得瀏覽器帶過來的所有Cookie:
request.getCookies() ; //得到所有的cookie物件,是一個陣列,開發中根據key得到目標cookie
- cookie的 API
cookie.getName() ; //回傳cookie中設定的key
cookie.getValue(); //回傳cookie中設定的value
入門代碼
//第一次是服務器通過response 回應給客戶端
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲得所有的cookie
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
//獲得name是akey的cookie物件, 以及它的value
if("akey".equals(cookie.getName())){
System.out.println(cookie.getValue());
}
//獲得cookie的name和value
//System.out.println(cookie.getName() + "=" + cookie.getValue());
}
}
//創建Cookie
Cookie aCookie = new Cookie("akey", "aaa");
Cookie bCookie = new Cookie("bkey", "bbb");
//寫給瀏覽器
response.addCookie(aCookie);
response.addCookie(bCookie);
response.getWriter().print("ServletDemo01");
}
小結
-
cookie特點
- Cookie保存在客戶端(瀏覽器端的)
- 第一次請求的時候, 沒有Cookie的, 先由服務器寫給瀏覽器.
- Cookie里面只能保存字串, 大小有限制
-
cookie相關API
- new Cookie(String name,String value); 創建Cookie
- response.addCookie(cookie); 把Cookie寫給瀏覽器
- request.getCookies(); 獲得所有的Cookie物件
- cookie.getName() 獲得Cookie的key
- cookie.getValue() 獲得Cookie的value
Cookie本質是請求頭,回應頭
-
我們一般根據cookie的name獲得目標cookie物件. 我們可以把這塊邏輯封裝到工具類里面
/**
* @Description: Cookie的工具類
* @Author: yp
*/
public class CookieUtils {
//根據key從所有的Cookies里面獲得目標Cookie物件
public static Cookie getTargetCookie(String key,Cookie[] cookies){
//1. 非空判斷
if(cookies == null){
return null;
}
//2.遍歷, 根據key獲得目標Cookie回傳
for (Cookie cookie : cookies) {
if(key.equals(cookie.getName())){
return cookie;
}
}
return null;
}
}
Cookie進階
cookie的分類
- 會話級別cookie
? 在默認的情況下,當瀏覽器行程結束(瀏覽器關閉,會話結束)的時候,cookie就會消失,
-
持久性cookie
? 給cookie設定有效期.
cookie.setMaxAge(int expiry):時間是秒? -1:默認,代表Cookie資料存到瀏覽器關閉(保存在瀏覽器檔案中),
正整數:以秒為單位保存資料有有效時間(把快取資料保存到磁盤中)? 0:代表洗掉Cookie.如果要洗掉Cookie要確保路徑一致,
cookie設定有效路徑
setPath(String url) ;設定路徑
? 有效路徑作用 :
- 保證不會攜帶別的網站/專案里面的cookie到我們自己的專案
- 如果路徑不一樣, cookie的key可以相同
- 保證自己的專案可以合理的利用自己專案的cookie
-
默認路徑,例如:
- 訪問http://localhost:8080/web18A_Cookie/demo01; cookie默認路徑 /web18A_Cookie
-
訪問http://localhost:8080/web18A_Cookie/aaa/demo01; cookie默認路徑 /web18A_Cookie/aaa
-
訪問http://localhost:8080/web18A_Cookie/aaa/bbb/demo01; cookie默認路徑 /web18A_Cookie/aaa/bbb
-
隨帶Cookie需要的條件: ==只有當訪問資源的url包含此cookie的有效path的時候,才會攜帶這個cookie;==反之不會.
-
eg: 設定cookie的路徑 /demo02
下次訪問路徑:http://localhost:8080/day30a-cookie/demo02/ccc; cookie是可以帶過來
下次訪問路徑:http://localhost:8080/day30a-cookie/demo03; cookie帶不過來
-
-
cookie的路徑通常設定 / 或者 /發布專案名設定的有效是 /day30a-cookie. 當前專案下的Servlet都可以使用該cookie. 一般這么設定: cookie.setPath(request.getContextPath());
只要是當前專案里面的資源 路徑必須包含專案名路徑.
小結
-
Cookie的型別
- 會話級別【默認的】 瀏覽器關閉了就消失了
- 持久級別
setMaxAge(int 秒)- -1 默認值
- 正整數
- 0 洗掉cookie 【必須路徑一致】
-
cookie有效路徑 cookie.setPath(String path) 建議設定成當前的專案部署路徑; setPath(request.getContextPath())
-
cookie的弊端 cookie的大小(個數和自身大小)和格式(只能存字串)有限制,默認不支持中文,解決中文辦法
URLEncode.encode(value,"utf-8");//存入的時候(先通過utf-8編碼)
URLDecode.decode(value,"utf-8");//取出 (通過utf-8解碼)
記錄用戶各自的上次訪問時間
1.需求
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-xles3B21-1619450555098)(img/tu_1.gif)]](https://img.uj5u.com/2021/04/29/240311291100134.png)
? 在訪問一個資源的時候,展示上次訪問的時間
? 若是第一次訪問則展示:你是第一次訪問,若不是第一次則展示:你上次訪問的時間是:xxxx
2.分析
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-CTXTZHEz-1619450555099)(img/image-20191211105647723.png)]](https://img.uj5u.com/2021/04/29/240311291100135.png)
3.代碼實作
package com.itheima.web;
import com.itheima.utils.CookieUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Description:
* @Author: yp
*/
@WebServlet("/rem")
public class RememberServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解決亂碼
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//1.判斷是否是第一次訪問(說白了判斷目標cookie是否為null)
Cookie[] cookies = request.getCookies();
Cookie targetCookie = CookieUtils.getTargetCookie("lastTime", cookies);
if(targetCookie == null){
//2.為null, 第一次訪問
//2.1記錄當前的時間到Cookie
Cookie cookie = new Cookie("lastTime", System.currentTimeMillis() + "");
cookie.setMaxAge(60*60*24);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
//2.2回應'你是第一次訪問'
response.getWriter().print("你是第一次訪問");
}else{
//3. 不為null 不是第一次訪問
//3.1 記錄當前的時間到Cookie
Cookie cookie = new Cookie("lastTime", System.currentTimeMillis() + "");
cookie.setMaxAge(60*60*24);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
//3.2 從cookie獲得上次的訪問的時間 回應
String timeStr = targetCookie.getValue();
Date date = new Date(Long.parseLong(timeStr));
response.getWriter().print("你上次訪問的時間是:"+date.toLocaleString());
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
第三章-Session
3.1session概述
?session是服務器端的技術,服務器為每一個瀏覽器開辟一塊記憶體空間,即session物件,由于session物件是每一個瀏覽器特有的,所有用戶的記錄可以存放在session物件中,同時,每一個session物件都對應一個sessionId,服務器把sessionId寫到cookie中,再次訪問的時候,瀏覽器把sessionId帶過來,找到對應的session物件
3.2cookie和Session的不同
- cookie是保存在瀏覽器端的,大小和個數都有限制,session是保存在服務器端的, 原則上大小是沒有限制(實際開發里面也不會存很大大小), 安全一些,
- cookie不支持中文,并且只能存盤字串;session可以存盤基本資料型別,集合,物件等
3.3Session的執行原理
? 1、獲得cookie中傳遞過來的SessionId(cookie)
? 2、如果Cookie中沒有sessionid,則創建session物件
? 3、如果Cookie中有sessionid,找指定的session物件
? 如果有sessionid并且session物件存在,則直接使用
? 如果有sessionid,但session物件銷毀了,則執行第二步
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-g4DfCNIA-1619450555100)(img/image-20191211112121400.png)]](https://img.uj5u.com/2021/04/29/240311291100136.png)
Session的基本使用
? 范圍: 會話(多次請求) 保存用戶各自的資料(以瀏覽器為單位)
- request.getSession(); 獲得session(如果第一次呼叫的時候其實是創建session,第一次之后通過sessionId找到session進行使用)
- Object getAttribute(String name) ;獲取值
- void setAttribute(String name, Object value) ;存盤值
- void removeAttribute(String name) ;移除
小結
-
Session基本使用: 作為域物件存取資料 范圍: 一次會話(多次請求, 用戶各自的) 不同的瀏覽器session不一樣
-
Object getAttribute(String name) ;獲取值
-
void setAttribute(String name, Object value) ;存盤值
-
void removeAttribute(String name) ;移除
-
-
瀏覽器關閉了, session使用不了, 是session銷毀了嗎?
? session沒有銷毀.
?session基于cookie, sessionId保存到cookie里面的, 默認情況下cookie是會話級別,瀏覽器關閉了cookie就是消失了,也就是說sessionId消失了, 從而找不到對應的session物件了, 就不能使用了.
? 解決: 自己獲得sessionId, 自己寫給瀏覽器 設定Cookie的有效時長, 這個Cookie的key必須:
JSESSIONID
三個域物件比較
1.三個域物件比較
| 域物件 | 創建 | 銷毀 | 作用范圍 | 應用場景 |
|---|---|---|---|---|
| ServletContext | 服務器啟動 | 服務器正常關閉/專案從服務器移除 | 整個專案 | 記錄訪問次數,聊天室 |
| HttpSession | 沒有session 調 用request.getSession()方法 | session過期(默認30分鐘)/呼叫invalidate()方法/服務器例外關閉 | 會話(多次請求) | 驗證碼校驗, 保存用戶登錄狀態等 |
| HttpServletRequest | 來了請求 | 回應這個請求(或者請求已經接收了) | 一次請求 | servletA和jsp(servletB)之間資料傳遞(轉發的時候存資料) |
C:\Users\yp\.IntelliJIdea2017.2\system\tomcat\_sz61\work\Catalina\localhost 目錄查看
- 如果是正常關閉服務器,
? 把session鈍化到服務器磁盤上,再次啟動,把磁盤上的檔案活化到記憶體里面
? Session鈍化:把記憶體中的session序列化到硬碟上
? Session活化:從硬碟上讀取序列化的session到記憶體中
2.三個域物件怎么選擇?
三個域物件怎么選擇?
? 一般情況下, 最小的可以解決就用最小的.
? 但是需要根據情況(eg: 重定向, 多次請求, 會話范圍, 用session; 如果是轉發,一般選擇request)
小結
- session的銷毀
- invalidate()
- session應用場景
- 驗證碼校驗
- 保存用戶的登錄狀態
- …
- 選擇
- 一般情況下, 最小的可以解決就用最小的.
- 轉發 一般選擇request
- 重定向 一般選擇session
- 一般情況下, 最小的可以解決就用最小的.
一次性驗證碼校驗
1.需求
? ![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-kCI6S62U-1619450555102)(img/tu_4.png)]](https://img.uj5u.com/2021/04/29/240311291100137.png)
? 在網站登錄的時候,生成一個驗證碼.登錄的時候對驗證碼進行校驗.
2.分析
2.1生成驗證碼
- 拷貝驗證碼的jar包
- 創建CodeServlet
//1.生成驗證碼
//2.回應給客戶端(瀏覽器)
2.2校驗驗證碼
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-19dXSISr-1619450555103)(img/image-20191211150240836.png)]](https://img.uj5u.com/2021/04/29/240311291100138.png)
3.實作
- 登錄頁面
<center>
<h1>用戶登錄</h1>
<form action="login" method="post">
姓名:<input type="text" name="username"/><br/>
密碼:<input type="password" name="password"/><br/>
驗證碼:<input type="text" name="code"/><br/>
<img src="code" onclick="changeImg(this)"/><br/>
<input type="submit" value="登錄"/>
</form>
</center>
<script>
function changeImg(obj) {
//obj.setAttribute("src","code");
//帶引數沒有實際的含義, 目的就是忽悠瀏覽器 路徑不一樣不要讀取快取
obj.src = "code?a="+new Date().getMilliseconds();
}
</script>
- CodeServlet
@WebServlet("/code")
public class CodeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.生成驗證碼 int width 寬度, int height 高度, int codeCount 驗證碼的個數, int lineCount 干擾線的條數
ValidateCode validateCode = new ValidateCode(200, 50, 4, 100);
//2.生成的驗證碼存到session
String code = validateCode.getCode();
System.out.println("生成的驗證碼:" + code);
request.getSession().setAttribute("code",code);
//3.回應給客戶端(瀏覽器)
validateCode.write(response.getOutputStream());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- LoginServlet
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//0.處理亂碼
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//***************驗證碼的校驗****************
//1.獲得用戶輸入的驗證碼
String userCode = request.getParameter("code");
//2.從session獲得程式生成的驗證碼
String sessionCode = (String) request.getSession().getAttribute("code");
//3.比較, 判斷是否一致
if(!userCode.equals(sessionCode)){
response.getWriter().print("驗證碼輸入有誤");
return;
}
//******************************************
//1.獲得用戶名和密碼
String username = request.getParameter("username");
String password = request.getParameter("password");
//2.使用DBUtils根據用戶名和密碼查詢資料庫 封裝成User物件
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
String sql = "SELECT * FROM user WHERE username = ? AND password =?";
User user = queryRunner.query(sql, new BeanHandler<User>(User.class), username, password);
//3.判斷是否登錄成功(判斷User是否為null)
if (user != null){
//3.1 user!= null 回應登錄成功
//把user保存到session里面
request.getSession().setAttribute("user",user);
//重定向到首頁
response.sendRedirect(request.getContextPath()+"/index.html");
}else{
//3.2 user== null 回應登錄失敗
response.getWriter().print("登錄失敗!");
}
} catch (Exception e) {
e.printStackTrace();
response.getWriter().print("登錄失敗!");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
4.小結
-
需要在CodeServlet 把生成的驗證碼存到session里面
- 不能存到ServletContext, 就共享驗證碼了
- 不能存到request, 根本不能用
-
思路
- CodeServlet: 生成驗證碼存到Session
- LoginServlet:
- 獲得用戶輸入的驗證碼
- 獲得session里面存的驗證碼
- 比較是否一致
第四章_JSP入門
4.1 什么是JSP
? Java server page(java服務器頁面). 【JSP本質就是Servlet 】
? 它和servle技術一樣,都是SUN公司定義的一種用于開發動態web資源的技術,
? JSP=html(js,css)+java+jsp特有的內容
4.2.JSP產生的原因
需求: 我們要向頁面動態輸出一個表格. 發現特別的繁瑣
servlet在展示頁面的時候,相當的繁瑣,sun公司為了解決這個問題,參照asp開發了一套動態網頁技術jsp,
4.3 JSP執行原理
C:\Users\yp.IntelliJIdea2017.2\system\tomcat_sz61\work\Catalina\localhost
JSP會翻譯(通過默認的JspServlet,JSP引擎)成Servlet(.java),Servlet編譯成class檔案
? JSP執行流程
? 第一次訪問的xxx.jsp時候,服務器收到請求,JspServlet會去查找對應的jsp檔案
? 找到之后,服務器會將這個jsp檔案轉換成java檔案(Servlet)
? 服務器編譯java檔案,生成class檔案
? 服務器運行class檔案,生成動態的內容
? 服務器收到內容之后,回傳給瀏覽器
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-EBVzNOY1-1619450555104)(img/tu_5.png)]](https://img.uj5u.com/2021/04/29/240311291100139.png)
小結
- JSP: java 服務器 頁面, sun公司定義的動態資源, 本質就是Servlet
- JSP產生的原因: Servlet在動態展示很麻煩, jsp展示方便一點
JSP基本語法
1.JSP腳本
我們可以通過JSP腳本在JSP頁面上撰寫Java代碼. 一共有三種方式:
| 型別 | 翻譯成Servlet對應的部分 | 注意 |
|---|---|---|
| <%…%>:Java程式片段 | 翻譯成Service()方法里面的內容, 區域的 | |
| <%=…%>:輸出運算式 | 翻譯成Service()方法里面的內容,相當于呼叫out.print() | 輸出運算式不能以;結尾 |
| <%!..%>:宣告成員變數 | 翻譯成Servlet類里面的內容 |
- eg
<%
for(int i = 0; i < 10;i++){
out.print("i="+i);
%>
<hr/>
<%
}
%>
2.JSP注釋
| 注釋型別 |
|---|
| HTML注釋 |
| JAVA注釋 //; /* */ |
| JSP注釋; <%–注釋內容–%> |
注釋快捷鍵:Ctrl+Shift+/
小結
- 腳本
- <%%> 翻譯成了service()方法里面的區域內容
- <%=%> 輸出, 翻譯成了service()方法里面的out.print()
- <%!%> 翻譯成了servlet類里面的全域內容
- 注釋
- 注釋快捷鍵:Ctrl+Shift+/
記住用戶名案例
1.需求
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-mE8ax85T-1619450555105)(img/image-20191120112234968.png)]](https://img.uj5u.com/2021/04/29/2403112911001310.png)
2.分析
-
在LoginServlet里面, 如果用戶登錄成功:
? //判斷用戶是否勾選了記住用戶名
? //勾選了, 把用戶名存到Cookie
-
在login.jsp頁面 從cookie取出展示
3.實作
- LoginServlet
package com.itheima.web;
import com.itheima.bean.User;
import com.itheima.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
/**
* @Description:
* @Author: yp
*/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//0.處理亂碼
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//***************驗證碼的校驗****************
//1.獲得用戶輸入的驗證碼
String userCode = request.getParameter("code");
//2.從session獲得程式生成的驗證碼
String sessionCode = (String) request.getSession().getAttribute("code");
//3.比較, 判斷是否一致
if(!userCode.equals(sessionCode)){
response.getWriter().print("驗證碼輸入有誤");
return;
}
//******************************************
//1.獲得用戶名和密碼
String username = request.getParameter("username");
String password = request.getParameter("password");
//2.使用DBUtils根據用戶名和密碼查詢資料庫 封裝成User物件
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
String sql = "SELECT * FROM user WHERE username = ? AND password =?";
User user = queryRunner.query(sql, new BeanHandler<User>(User.class), username, password);
//3.判斷是否登錄成功(判斷User是否為null)
if (user != null){
//3.1 user!= null 回應登錄成功
//****************記住用戶名*********************
//判斷用戶是否勾選了記住用戶名
String rem = request.getParameter("rem");
if(rem != null && "ok".equals(rem)){
//勾選了, 把用戶名存到Cookie
Cookie cookie = new Cookie("username", username);
cookie.setMaxAge(60*60*24*7);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
}else {
//沒有勾選 洗掉cookie
Cookie cookie = new Cookie("username", "");
cookie.setMaxAge(0);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
}
//***********************************************
//把user保存到session里面
request.getSession().setAttribute("user",user);
//重定向到首頁
response.sendRedirect(request.getContextPath()+"/index.html");
}else{
//3.2 user== null 回應登錄失敗
response.getWriter().print("登錄失敗!");
}
} catch (Exception e) {
e.printStackTrace();
response.getWriter().print("登錄失敗!");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- login.jsp
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-wmoMOmUc-1619450555106)(img/image-20191120112343353.png)]](https://img.uj5u.com/2021/04/29/2403112911001311.png)
小結
- 用戶勾選了記住用戶名,我們把用戶名存到Cookie里面
- 在login.jsp里面 從cookie取出用戶名展示
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/281239.html
標籤:java
上一篇:深度決議JVM記憶體模型
下一篇:Java例外簡單理解
