zoukankan      html  css  js  c++  java
  • 登录页面“记住我”的功能实现

    记住我的功能主要是cookie的一个应用,功能实现中要注意的是,对保存的中文时乱码的处理及cookie的保存路径设置。

    登录页面login.jsp:

    <%@page import="java.net.URLDecoder"%>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    String password="";
    String name="";
    String checked="";
    Cookie[] cookies = request.getCookies();        //取出cookie对象组
    for(int i = 0; cookies != null && i < cookies.length;i++){
             Cookie cookie = cookies[i];       //  取出其中的一个对象,含有name ,value
             if(cookie != null && "name".equals(cookie.getName())){      //获取第一个cookie对象的name
                 name = URLDecoder.decode(cookie.getValue(), "UTF-8");//进行解码
                 checked = "checked";
             }
             if(cookie != null && "password".equals(cookie.getName())){
                 password = cookie.getValue();
             }
    }
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'login.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
    
      <form action="system/testajax_cookie.do" method="POST">
      用户名:<input name="name" value="<%=name%>"/><br>
      密    码:<input type="password" name="password" value="<%=password%>"/><br>
      记住我:<input type="checkbox" name="remember" value="yes" <%=checked%>/><br>
      <input type="submit" value="submit"/><br>
      </form>
      </body>
    </html>

    MyUtil.java:

    package cn.itcast.elec.web.action;
    
    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
    
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class MyUtil {
        public static void remember(HttpServletRequest request,HttpServletResponse response){
            String name = request.getParameter("name");                    //获取用户名
            String password = request.getParameter("password");        //获取密码
            String remember = request.getParameter("remember");     //获取是否打钩
            String codeName="";
            try {
                codeName = URLEncoder.encode(name, "UTF-8");      //对输入的中文进行编码,防止乱码出现
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            Cookie nameCookie = new Cookie("name", codeName); 
            Cookie passwordCookie = new Cookie("password", password); 
            nameCookie.setPath(request.getContextPath()+"/");      //设置Cookie的有效路径
            passwordCookie.setPath(request.getContextPath()+"/");//设置Cookie的有效路径
             if(remember != null && "yes".equals(remember)){            //有记住我,就设置cookie的保存时间
                nameCookie.setMaxAge(7*24*60*60);
                passwordCookie.setMaxAge(7*24*60*60);
            }else{                                                                                 //没有记住我,设置cookie的时间为0
                nameCookie.setMaxAge(0);
                passwordCookie.setMaxAge(0);
            }
            response.addCookie(nameCookie);
            response.addCookie(passwordCookie);
        }
    }
  • 相关阅读:
    水壶-[Kruskal重构树] [解题报告]
    线性求逆元推导
    边界线与两端对齐
    左边竖条的实现方法
    $.ajax()知识
    area热点区域
    AJAX与XMLHttpRequest
    js运行机制
    优先级
    各种图形
  • 原文地址:https://www.cnblogs.com/wqsbk/p/6083239.html
Copyright © 2011-2022 走看看