準(zhǔn)備:SSM框架,MySQL數(shù)據(jù)庫(kù)
創(chuàng)新互聯(lián)建站長(zhǎng)期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為望花企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè),望花網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
用戶表 user
實(shí)體類:
public class User { /** * 主鍵id */ private Integer userId; /** * 賬號(hào) */ 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
@Service public 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, "用戶名或密碼錯(cuò)誤"); } return ResultVO.error(3, "用戶名或密碼不能為空"); } }
Controller
@RestController public 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> <div class="container"> <div><img src="img/login_bg_pic.jpg"></div> <form action="getUserByname" method="post" id="loginForm"> <div class="form-group"> <label for="exampleInputEmail1">用戶名</label> <input type="text" class="form-control" id="exampleInputEmail1" name="userId" autocomplete="off" value="${userId}"> </div> <div class="form-group"> <label for="exampleInputPassword1">密碼</label> <input type="password" class="form-control" id="exampleInputPassword1" name="password" value="${password}"> </div> <%--驗(yàn)證碼--%> <div class="form-group"> <div class="input-icon" > <i class="fa fa-picture-o"></i> <input class="form-control" type="text" id="verifyCode" name="verifyCode" placeholder="驗(yàn)證碼" maxlength="4" autocomplete="off"> <img src="${pageContext.request.contextPath }/getVerifyCode" width="110" height="34" id="verifyCodeImage"> </div> </div> <div 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> </div> <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> <div class="nice"> 歡迎來(lái)到:<br>教務(wù)綜合信息服務(wù)平臺(tái) </div> </form> <script src="/js/jquery-3.4.1.min.js"></script> <script src="/bootstrap4/js/bootstrap.min.js"></script> <script> $(function () { $(".login").click(function () { //發(fā)送ajax請(qǐng)求 $.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("<div>" + res.message + "</div>"); } } }); }); }); </script> </div> </body> </html>
總結(jié)
以上所述是小編給大家介紹的JAVA記住密碼功能的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
網(wǎng)站欄目:JAVA記住密碼功能的實(shí)現(xiàn)代碼
文章路徑:http://muchs.cn/article44/igeohe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、服務(wù)器托管、全網(wǎng)營(yíng)銷推廣、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)