The function of remembering account passwords is required in graduation design. I found a solution online, and I can modify it myself and the following method is the following.
First, the login page. When the user checks the password, pass it to the controller (the SSM framework I use), set the value of the cookie in the background, and then you don’t need to enter your account and password again when you log in next time.
Login.jsp code:
<%@page import="org.apache.commons.lang.StringUtils"%> <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@include file="public/nocache.jsp" %> <%@include file="public/header.jsp" %> <!-- Introduce related js --> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script> <style> body{ margin:0px; padding:0px; } .wrapper{ width:100%; height:100%; position:fixed; } .content{ width:100%; height:100%; position:relative; text-align:center; } .login{ width:1050px; height:450px; position:absolute; top:50%; left:50%; margin-top:-225px; margin-left:-525px; } </style> <script type="text/javascript"> window.history.forward(); window.onbeforeunload=function (){ } </script> <%@include file="public/headertop.jsp" %> <!-- Enter the resource file--> <body> <%-- Read cookie --%> <% String name = ""; String password = ""; try{ Cookie[] cookies = request.getCookies(); if(cookies!=null){ for(int i = 0;i<cookies.length;i++){ if(cookies[i].getName().equals("cookie_user")){ String values = cookies[i].getValue(); // If the value field is not empty if(StringUtils.isNotBlank(values)){ String[] elements = values.split("-"); // Get the account name or password if(StringUtils.isNotBlank(elements[0])){ name = elements[0]; } if(StringUtils.isNotBlank(elements[1])){ password = elements[1]; } } } } } } } } } } } } } catch(Exception e){ } %> <div style=""> <div> <div> <!-- The main content part begins --> <div fit="true"> <div region="west"> <div fit="true"> <div region="west"> </div> <div region="center" style="font-family:'Microsoft Yahei';"> <p style="position: relative;margin-top:200px;padding-left:20px;"> <span style="font-size:30px;font-weight:800;">Auto maintenance management system</span><br/> <span>Vehicle Maintenance Management System</span> </p> </div> </div> </div> <div region="center"> <div fit="true"> <div region="north" style="height:80px;"> </div> <div region="west"> <img src="${pageContext.request.contextPath}/img/split.png" style="position:absolute;left:0px;top:30px;"/> </div> <div region="center"> <div iconCls="icon-user" style="text-align: center;width:300px;height:260px;padding-top:50px;"> <form id="ff" method="post"> <div> <input id="account" name="accountnumber" data-options="iconCls:'icon-man',prompt:'Please enter the username'" value="<%=name %>" > <a id="dd" href="#"></a> </div> <div style="margin-top: 20px;"> <input id="passwords" name="passwords" type="password" data-options="iconCls:'icon-lock',prompt:'Please enter your password'" value="<%=password %>" > </div> <div style="margin-top: 10px;" style="text-align:left;" > <span style="float:left;padding-left:30px;font-size:12px;"><input id="flag" name="flag" type="checkbox" value="1" checked="checked" />Remember the account</span> </div> <div style="clear:both;"></div> <div style="margin-top: 20px;"> <p> <a href="#" id="submitbtn" iconCls="icon-accept">Login</a> <a style="margin-left:30px;width:80px;height:30px;" href="#" iconCls="icon-arrow_undo">Cancel</a> </p> </div> </form> </div> </div> <div region="east"> </div> <div region="south" style="height:0px;"> </div> </div> </div> </div> <!-- The main content part ends --> </div> </div> </div> </div> <script type="text/javascript"> $(function(){ console.log("[Automotive maintenance management system/n codeby:pengchan/n email:[email protected] /n csdn:http://blog.csdn.net/w3chhhhh/]"); // Submit form $("#submitbtn").click(function(){ // Determine whether it is empty if($("#account").val()==""){ $.messager.alert('Login message prompt','The user's account cannot be empty','info'); return; } if($("#passwords").val()==""){ $.messager.alert('Login message prompt','User's password cannot be empty','info'); return; } $("#ff").submit(); }); $('#ff').form({ url:"${pageContext.request.contextPath}/users/login.html", success:function(data){ data = JSON.parse(data); try{ if(data.isError){ $.messager.alert('Login message prompt',data.errorMsg,'info'); }else{ //$.messager.alert("Login message prompt","Login successfully!",'info'); // If successful, jump to the main page// Here we first determine whether there is a historical request. If there is, visit the previous page again var hisurl = '${hisURL}'; if(hisurl!=null&&hisurl.length>0){ window.location.href="${pageContext.request.contextPath}"+hisurl; }else{ window.location.href="${pageContext.request.contextPath}/index/main.html"; } } } } catch(e){ $.messager.alert("Login message prompt",'The server returns an exception, please try again later!','info'); } }, error:function(error){ $("#ff").form("clear"); } }); }); </script> </body> </html>Part of the java code processed in the background:
package com.javaweb.controller; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSON; import com.javaweb.entity.Account; import com.javaweb.service.impl.ServiceFactory; import com.javaweb.utils.BaseController; import com.javaweb.views.LoginBean; /** * User Information Controller* @author cp * */ @Controller @Scope("prototype") @RequestMapping("/users") public class UserInfoController extends BaseController{ private static final Logger logger = LoggerFactory.getLogger(UserInfoController.class); @Autowired private ServiceFactory serviceFactory; /** * Log in to the system* @param request Request* @param model model * @param account Account information* @return */ @RequestMapping("/login") @ResponseBody public String login(HttpServletRequest request,HttpServletResponse response,Model model,Account account){ logger.info("User trying to log in: "+JSON.toJSONString(account)); if(account==null){ return responseFail("The submitted parameter is empty!"); }else{ if(StringUtils.isBlank(account.getAccountnumber())){ return responseFail("User's account is empty"); } if(StringUtils.isBlank(account.getPasswords())){ return responseFail("User's password is empty"); } LoginBean loginBean = null; loginBean = serviceFactory.getUserValidateService().userislawable(account); if(loginBean==null){ return responseFail("Username or password input is incorrect"); }else{// If successful // Put loginbean in session request.getSession().setAttribute("user", loginBean); // Put it in cookie String flag = request.getParameter("flag"); // If you need to remember the account, store the account and password if(flag!=null&&flag.equals("1")){ Cookie cookie = new Cookie("cookie_user",loginBean.getAccountnumber()+"-"+loginBean.getPasswords()); cookie.setMaxAge(60*60*24*3);// Save response.addCookie(cookie); logger.info("Storage user's cookies:"+loginBean.getAccountnumber()+"-"+loginBean.getPasswords()); }else{// If you do not require to remember the account password, save the account Cookie cookie = new Cookie("cookie_user", loginBean.getAccountnumber()); cookie.setMaxAge(60*60*24*30); response.addCookie(cookie); logger.info("Storing the user's cookie:"+loginBean.getAccountnumber()); } // Jump to the home page logger.info("User:"+loginBean.getAccountnumber()+"Successfully entered the system"); return responseSuccess(loginBean, "LoginSuccessfully"); } } } /** * Log out of the system* @param request request* @param model model* @param account number* @return */ @RequestMapping("/{accountnum}/logout.html") public String logout(HttpServletRequest request,Model model,@PathVariable("accountnum") String accountnum){ logger.info("user"+accountnum+", log out of the system..."); // Set session to empty request.getSession().setAttribute("user", null); // Page jump return "login"; } }Running effect:
After entering your account password, log in:
Log in again after logging out:
The above is the editor’s introduction to you. Use cookies in the java web to remember the user’s account and password. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!