網(wǎng)站首頁 編程語言 正文
記錄一下java學(xué)習(xí)過程中的bug
cookie.setPath、setMaxAge都設(shè)置了,但是在登錄頁面讀取不到添加的cookie信息
后端LoginServlet界面
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接收數(shù)據(jù)
String username = request.getParameter("username");
String password = request.getParameter("password");
//從ServletContext域中獲得保存用戶信息集合;
List<User> list= (List<User>) this.getServletContext().getAttribute("list");
for(User user:list) {
//判斷用戶名是否正確
if(username.equals(user.getUsername())) {
//用戶名是正確的
if(password.equals(user.getPassword())) {
//密碼也正確
//登錄成功
//將用戶信息保存到session中:
request.getSession().setAttribute("user", user);
response.sendRedirect(request.getContextPath()+"/success.jsp");
//完成記住用戶名的功能
String remeber= request.getParameter("remeber");
if("true".equals(remeber)) {
Cookie cookie = new Cookie("username",user.getUsername());
//設(shè)置有效的路徑和有效時間
cookie.setPath("/reg_login");
cookie.setMaxAge(60*60*24);//24hour
//將cookie回寫到瀏覽器
response.addCookie(cookie);
}
return;
}
}
}
//登陸失敗;
request.setAttribute("msg", "用戶名或密碼錯誤");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
前端Login.jsp界面
<%
String username = "";
//獲得從客戶端攜帶過來的所有Cookie
Cookie[] cookies = request.getCookies();
//從Cookie的數(shù)組中查找指定名稱的Cookie
Cookie cookie = CookieUtils.findCookie(cookies,"username");
if(cookie !=null){
username = cookie.getValue();
}
%>
然后cookie信息不起作用,在login.jsp頁面添加打印cookie信息代碼,發(fā)現(xiàn)LoginServet中cookie信息并沒有回傳到瀏覽器,也就是說response.addCookie(cookie);沒有起作用。
<%
String userName = "";
//獲得從客戶端攜帶過來的所有cookie
Cookie[]cookies = request.getCookies();
//從Cookie的數(shù)組中查找指定名稱的Cookie
for(Cookie cookie:cookies){
System.out.println("cookie.Name:"+cookie.getName());
System.out.println("cookie.value:"+cookie.getValue());
if(cookie.getName().equals("userName")){
userName = cookie.getValue();
}
}
%>
搜了好長時間沒找到想要的解決方案,最終在https://www.cnblogs.com/woestave/p/6260526.html中找到了答案。
錯誤:
因為在LoginServlet頁面中,登錄成功后response.sendRedirect(request.getContextPath()+"/success.jsp"); 已經(jīng)把請求轉(zhuǎn)發(fā)給另一個頁面了,所以雖然也創(chuàng)建了cookie,并能輸出cookie.getName(),但是response已經(jīng)不具備向瀏覽器響應(yīng)請求的能力了,response.addCookie(cookie)也就不起作用
解決方案:
for(User user:list){
if(user.getUsername().equals(userName)){
if(user.getPassword().equals(password)){
//完成記住用戶名的功能
String remember = request.getParameter("remember");
if("true".equals(remember)){
Cookie cookie = new Cookie("userName",userName);
cookie.setPath(request.getContextPath());
cookie.setMaxAge(60*60*24);
response.addCookie(cookie);
}
//登錄成功,重定向到成功頁面
request.getSession().setAttribute("user",user);
response.sendRedirect(request.getContextPath()+"/success.jsp");
return;
}
}
}
原文鏈接:https://blog.csdn.net/cpt_ljy/article/details/103764352
相關(guān)推薦
- 2022-01-15 解決npm install 報錯 npm ERR! code 128 npm ERR! comman
- 2022-05-22 基于jQuery排序及應(yīng)用實現(xiàn)Tab欄特效_jquery
- 2023-01-15 PyQt5+QtChart實現(xiàn)柱狀圖的繪制_python
- 2024-02-28 UNI-APP,實現(xiàn)給字符串中部分字符、子串添加樣式(顏色,字號等)
- 2022-06-09 Python列表的索引與切片_python
- 2022-10-16 Qt實現(xiàn)進(jìn)程間通信_C 語言
- 2022-05-04 分享3個非常實用的?Python?模塊_python
- 2022-01-22 C++四種強制類型轉(zhuǎn)換
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支