The attribute name s corresponding to each field are as follows:
Cookie s are processed after the user first logs in
Landing Processing:
Cookie processing:
- /**Land*/
- public String login() throws Exception{
- //Processing Verification Code: Determining whether the input of Verification Code is correct
- boolean flag=VerificationCodeUtil.isCheckNum(request);
- if(!flag){
- this.addFieldError("checkNum", "Error in Verification Code");
- return "loginUI";
- }
- User user=userService.findByLoginNameAndPassword(model.getName(),model.getPassword());
- if(user==null){
- addFieldError("login", "Username or password incorrect");
- return "loginUI";
- }else{
- ActionContext.getContext().getSession().put("user", user);
- //Processing Cookie
- addCookie(model.getName(),model.getPassword(),response,request);
- return "toIndex";
- }
- }
- /**Cookie Realization**/
- private void addCookie(String name, String password,HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
- if(StringUtils.isNotBlank(name)&&StringUtils.isNotBlank(password)){
- //Create Cookie
- Cookie nameCookie=new Cookie("name",URLEncoder.encode(name,"utf-8"));
- Cookie pswCookie=new Cookie("psw",password);
- //Setting the parent path of Cookie
- nameCookie.setPath(request.getContextPath()+"/");
- pswCookie.setPath(request.getContextPath()+"/");
- //Get whether to save Cookie or not
- String rememberMe=request.getParameter("rememberMe");
- if(rememberMe==null){//Do not save Cookie
- nameCookie.setMaxAge(0);
- pswCookie.setMaxAge(0);
- }else{//The length of time to save cookies in seconds
- nameCookie.setMaxAge(7*24*60*60);
- pswCookie.setMaxAge(7*24*60*60);
- }
- //Add Cookie to Response Header
- response.addCookie(nameCookie);
- response.addCookie(pswCookie);
- }
- }
- <%
- String name="";
- String psw="";
- String checked="";
- Cookie[] cookies=request.getCookies();
- if(cookies!=null&&cookies.length>0){
- //Traversing Cookie
- for(int i=0;i<cookies.length;i++){
- Cookie cookie=cookies[i];
- //Similar to Map, there are two fields named and value, name is equal before assignment, and the encoding problem is handled.
- if("name".equals(cookie.getName())){
- name=URLDecoder.decode(cookie.getValue(),"utf-8");
- //Set "Remember Me" to Check
- checked="checked";
- }
- if("psw".equals(cookie.getName())){
- psw=cookie.getValue();
- }
- }
- }
- %>
Finally, the user name, password, remember to add value to my field.
- <TR>
- <TD>User Account:</TD>
- <TD><input name="name" type="text" value="<%=name %>" id="name"/></TD>
- </TR>
- <TR>
- <TD>Login password:</TD>
- <TD><input name="password" type="password" value="<%=psw %>" id="password"></TD>
- </TR>
- <TR>
- <TD>Remember me:</TD>
- <TD><input name="rememberMe" type="checkbox" id="rememberMe" <%=checked %>></TD>
- </TR>
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.