您好,登錄后才能下訂單哦!
本篇文章為大家展示了JAVA中怎么實現記住密碼功能,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
實體類:
public class User { /** * 主鍵id */ private Integer userId; /** * 賬號 */ private String username; /** * 密碼 */ private String password; public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
UserMapper
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- 命名空間 --><mapper namespace="com.luowx.mapper.UserMapper"> <resultMap id="userMap" type="User"> <id property="userId" column="user_id"/> <result property="username" column="username"/> <result property="password" column="password"/> </resultMap> <select id="getUserByname" resultMap="userMap"> select * from s_user where username=#{username} </select></mapper>
mapper層
public interface UserMapper { User getUserByname(String username);}
service層
public interface UserService { User getUserByname(String username, String password, HttpSession session, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse);}
impl
@Servicepublic class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public ResultVO getUserByname(String username, String password, HttpSession session, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { String remember = httpServletRequest.getParameter("remember"); if (username!= null && username!= 0){ User user = userMapper.getUserByname(username); if (user != null && user.getPassword().equals(password)){ session.setAttribute("user", user); if (remember != null){ Cookie cookieUser = new Cookie("username", username); Cookie cookiePass = new Cookie("password", password); cookieUser.setMaxAge(60 * 60 * 24); cookiePass.setMaxAge(60 * 60 * 24); httpServletResponse.addCookie(cookieUser); httpServletResponse.addCookie(cookiePass); return ResultVO.success(user); } return ResultVO.error(1, "用戶名或密碼錯誤"); } return ResultVO.error(3, "用戶名或密碼不能為空"); }}
Controller
@RestControllerpublic class UserController { @Autowired private UserService userService; //登錄 @RequestMapping("/getUserByname") public ResultVO getUserByname(String username, String password, HttpSession session, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){ return userService.getUserByname(username, password, session, httpServletRequest, httpServletResponse); }}
前端代碼(JSP)
html的自己修改下,樣式是BootStrap的
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>登錄</title> <link rel="stylesheet" href="/bootstrap4/css/bootstrap.min.css" rel="external nofollow" > <style> body{ background-color: #fafafa; } .nice{ position: relative; display: flex; align-items: center; justify-content: center; } .container{ margin-top: 100px; display: flex; justify-content: space-between; border: #b3b7bb 2px solid; border-radius: 5px; } #loginForm{ width: 300px; background-color: rgba(255,255,255,0.7); margin-top: 30px; border: #b3b7bb 1px solid; border-radius: 5px; } </style></head><body><p class="container"> <p><img src="img/login_bg_pic.jpg"></p> <form action="getUserByname" method="post" id="loginForm"> <p class="form-group"> <label for="exampleInputEmail1">用戶名</label> <input type="text" class="form-control" id="exampleInputEmail1" name="userId" autocomplete="off" value="${userId}"> </p> <p class="form-group"> <label for="exampleInputPassword1">密碼</label> <input type="password" class="form-control" id="exampleInputPassword1" name="password" value="${password}"> </p> <%--驗證碼--%> <p class="form-group"> <p class="input-icon" > <i class="fa fa-picture-o"></i> <input class="form-control" type="text" id="verifyCode" name="verifyCode" placeholder="驗證碼" maxlength="4" autocomplete="off"> <img src="${pageContext.request.contextPath }/getVerifyCode" width="110" height="34" id="verifyCodeImage"> </p> </p> <p class="custom-control custom-checkbox mb-3 was-validated"> <input type="checkbox" class="custom-control-input" id="rem" name="remember" checked> <label class="custom-control-label" for="rem">記住密碼</label> </p> <button type="button" class="btn btn-primary login">登 錄</button> <a href="/forgotpass" rel="external nofollow" ><button type="button" class="btn btn-danger">忘記密碼</button></a> <br><br> <p class="nice"> 歡迎來到:<br>教務綜合信息服務平臺 </p> </form> <script src="/js/jquery-3.4.1.min.js"></script> <script src="/bootstrap4/js/bootstrap.min.js"></script> <script> $(function () { $(".login").click(function () { //發送ajax請求 $.ajax({ url:'getUserByname', type:'post', data:$("#loginForm").serialize(), success:function (res) { console.log(res); if (res.status === 0){ if(res.data.role === 0) { location.href = "overview"; }else if (res.data.role === 1){ location.href = "teacher"; }else if (res.data.role === 2){ location.href = "teacher"; }else if (res.data.role === 3){ location.href = "student"; } } else { $(".nice").html("<p>" + res.message + "</p>"); } } }); }); }); </script></p></body></html>
上述內容就是JAVA中怎么實現記住密碼功能,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。