日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用session保持登录状态,cookie保存用户账号密码

發(fā)布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用session保持登录状态,cookie保存用户账号密码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

session保存登錄狀態(tài)與cookie保存賬號密碼

  • 1. session維持登錄狀態(tài)
    • 1.1 代碼實(shí)現(xiàn)
  • 2. cookie保存賬號密碼
    • 2.1 什么是cookie
    • 2.2 cookie記住賬號密碼

1. session維持登錄狀態(tài)

  • 利用session的生命周期實(shí)現(xiàn)

1.1 代碼實(shí)現(xiàn)

  • login頁面表單部分
<form action="judgeLoginSession" method="post">name: <input type="text" name="name" /> <br/>password:<input type="password" name="pwd" /> <br/><input type="submit" value="提交"/></form>
  • index頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><script type="text/javascript">window.onload = function() {if (${empty name}) {alert("請登錄!");location.href = "login.jsp";}};</script></head><body> 歡迎${ name },登錄成功!</body> </html>
  • 后臺為了簡單就不搞數(shù)據(jù)庫了
protected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {String name = req.getParameter("name");String pwd = req.getParameter("pwd");if ("rye".equals(name) && "123".equals(pwd)) {// 登錄成功 session 保存姓名req.getSession().setAttribute("name", name);resp.sendRedirect("index.jsp");} else // 賬號密碼錯誤重新登錄。resp.sendRedirect("login.jsp");}
  • 訪問首頁,js判斷沒有登錄
  • 測試
  • 登錄成功,跳轉(zhuǎn)首頁

  • 關(guān)閉首頁,重新訪問,能取到名字,session存在。

2. cookie保存賬號密碼

2.1 什么是cookie

cookie的基本使用

2.2 cookie記住賬號密碼

  • 登錄頁面
<form action="testCookie" method="post">name: <input type="text" name="name" value="${ cookie.name.value }"/> <br/>password:<input type="password" name="pwd" value="${ cookie.pwd.value }"/> <br/>記住密碼: <input type="checkbox" name="remPwd" value="1"/><input type="submit" value="提交"/></form>
  • 后臺取到寫cookie到客戶端
protected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {String name = req.getParameter("name");String pwd = req.getParameter("pwd");// 標(biāo)記是否記住密碼String remPwd = req.getParameter("remPwd");// 登錄成功 if ("rye".equals(name) && "123".equals(pwd)) {if (remPwd != null) { // 記住密碼// cookie 保存姓名, 密碼Cookie cookie1 = new Cookie("name", name);Cookie cookie2 = new Cookie("pwd", pwd);// cookie存在瀏覽器一個小時, 保存瀏覽器的文件下cookie1.setMaxAge(60*60);// 保存cookie到瀏覽器resp.addCookie(cookie1);resp.addCookie(cookie2);}resp.sendRedirect("MyJsp.jsp");} else // 賬號密碼錯誤重新登錄。resp.sendRedirect("login.jsp");}
  • 測試
  • 登錄成功,跳轉(zhuǎn)

  • 菜鳥f12,cookie如下,服務(wù)器的響應(yīng)如下

我是bitQian,如果你覺得我寫得還闊以,點(diǎn)贊,評論,分享三連擊。

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的使用session保持登录状态,cookie保存用户账号密码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。