Login page to save account password

Implementing "Remember Me" through Cookie 1. The landing page is as follows: The attribute name s corresponding to each field are as follow...
Implementing "Remember Me" through Cookie 1. The landing page is as follows:


The attribute name s corresponding to each field are as follows:

User Account name Login password password Verification Code checkNum Remember me rememberMe

2. The treatment in Action is as follows:

Cookie s are processed after the user first logs in

Landing Processing:

  1. /**Land*/
  2. public String login() throws Exception{
  3. //Processing Verification Code: Determining whether the input of Verification Code is correct
  4. boolean flag=VerificationCodeUtil.isCheckNum(request);
  5. if(!flag){
  6. this.addFieldError("checkNum", "Error in Verification Code");
  7. return "loginUI";
  8. }
  9. User user=userService.findByLoginNameAndPassword(model.getName(),model.getPassword());
  10. if(user==null){
  11. addFieldError("login", "Username or password incorrect");
  12. return "loginUI";
  13. }else{
  14. ActionContext.getContext().getSession().put("user", user);
  15. //Processing Cookie
  16. addCookie(model.getName(),model.getPassword(),response,request);
  17. return "toIndex";
  18. }
  19. }
Cookie processing:

  1. /**Cookie Realization**/
  2. private void addCookie(String name, String password,HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
  3. if(StringUtils.isNotBlank(name)&&StringUtils.isNotBlank(password)){
  4. //Create Cookie
  5. Cookie nameCookie=new Cookie("name",URLEncoder.encode(name,"utf-8"));
  6. Cookie pswCookie=new Cookie("psw",password);
  7. //Setting the parent path of Cookie
  8. nameCookie.setPath(request.getContextPath()+"/");
  9. pswCookie.setPath(request.getContextPath()+"/");
  10. //Get whether to save Cookie or not
  11. String rememberMe=request.getParameter("rememberMe");
  12. if(rememberMe==null){//Do not save Cookie
  13. nameCookie.setMaxAge(0);
  14. pswCookie.setMaxAge(0);
  15. }else{//The length of time to save cookies in seconds
  16. nameCookie.setMaxAge(7*24*60*60);
  17. pswCookie.setMaxAge(7*24*60*60);
  18. }
  19. //Add Cookie to Response Header
  20. response.addCookie(nameCookie);
  21. response.addCookie(pswCookie);
  22. }
  23. }


3.JSP pages are processed as follows: Add the following code to the login JSP page to get the user name and password and fill in automatically:

  1. <%
  2. String name="";
  3. String psw="";
  4. String checked="";
  5. Cookie[] cookies=request.getCookies();
  6. if(cookies!=null&&cookies.length>0){
  7. //Traversing Cookie
  8. for(int i=0;i<cookies.length;i++){
  9. Cookie cookie=cookies[i];
  10. //Similar to Map, there are two fields named and value, name is equal before assignment, and the encoding problem is handled.
  11. if("name".equals(cookie.getName())){
  12. name=URLDecoder.decode(cookie.getValue(),"utf-8");
  13. //Set "Remember Me" to Check
  14. checked="checked";
  15. }
  16. if("psw".equals(cookie.getName())){
  17. psw=cookie.getValue();
  18. }
  19. }
  20. }
  21. %>

Finally, the user name, password, remember to add value to my field.

  1. <TR>
  2. <TD>User Account:</TD>
  3. <TD><input name="name" type="text" value="<%=name %>" id="name"/></TD>
  4. </TR>
  5. <TR>
  6. <TD>Login password:</TD>
  7. <TD><input name="password" type="password" value="<%=psw %>" id="password"></TD>
  8. </TR>
  9. <TR>
  10. <TD>Remember me:</TD>
  11. <TD><input name="rememberMe" type="checkbox" id="rememberMe" <%=checked %>></TD>
  12. </TR>


4. Possible problems If the user name and password are not automatically filled in, it may be an address problem:

Solution:

1. Enter the Internet option and click on the settings in the Browse History column


2. After entering the browsing record settings, Click to view the files


3. Find the Cookie file in the pop-up folder, whose name is your project name +"/", and the format is as follows. This file is usually copied to the desktop at the end of the folder. Note: This file can not be opened directly in the folder.



4. As shown below, the third action saves the path of the project project project. At this time, in Cookie processing in Action, the corresponding Cookie parent path can be set according to the address, which has been set above.

8 July 2019, 20:46 | Views: 3456

Add new comment

For adding a comment, please log in
or create account

0 comments