记住我

作用:记住当前用户名和密码,下次登录名不需要用户再次输入

 

第一步:index.sp页面

<tr>

    <td width="100"><img border="0" src="${pageContext.request.contextPath}/images/remeber.jpg" width="75" height="20"></td>

    <td>

        <input type="checkbox" name="remeberMe" id="remeberMe" value="yes"/>

    </td>

</tr>

 

第二步:Action代码的处理,创建LoginUtils类

public class LogonUtils {

 

    /**记住我功能*/

    public static void remeberMe(String name, String password,

            HttpServletRequest request, HttpServletResponse response) {

        //1:创建2个Cookie,存放指定值

        Cookie nameCookie = new Cookie("name",name);

        Cookie passwordCookie = new Cookie("password",password);

        //2:设置Cookie的有效路径(指定当前项目)

        nameCookie.setPath(request.getContextPath()+"/");

        passwordCookie.setPath(request.getContextPath()+"/");

        //3:设置Cookie的有效时间(1周)

        //获取页面复选框的值(用作判断)

        String remeberMe = request.getParameter("remeberMe");

        //此时表示复选框选中

        if(remeberMe!=null && remeberMe.equals("yes")){

            nameCookie.setMaxAge(7*24*60*60);

            passwordCookie.setMaxAge(7*24*60*60);

        }

        //此时表示复选框没有被选中

        else{

            nameCookie.setMaxAge(0);

            passwordCookie.setMaxAge(0);

        }

        //4:将Cookie存放到response中

        response.addCookie(nameCookie);

        response.addCookie(passwordCookie);

    }

}

 

第三步:在index.jsp页面中读取Cookie中的数据,jsp中嵌套java代码

<%

String name = "";

String password = "";

String checked = "";

Cookie [] cookies = request.getCookies();

if(cookies!=null && cookies.length>0){

    for(Cookie cookie:cookies){

        if(cookie.getName().equals("name")){

            name = cookie.getValue();

            checked = "checked";

        }

        if(cookie.getName().equals("password")){

            password = cookie.getValue();

        }

    }

}

%>

缺点:将java代码放置到jsp上,要求jsp先要执行编译java代码,然后再执行。效率会降低,能否将java代码抽取出去呢?

分析:在跳转到index.jsp页面之前先从Cookie中获取数据,放置到HttpRequest对象中进行显示,这样可以使用过滤器(filter)完成:

 

第四步:添加过滤器

public class SystemFilter implements Filter {

 

    /**web容器启动的时候,执行的方法*/

    public void init(FilterConfig config) throws ServletException {

 

    }

   

    /**每次访问URL连接的时候,先执行过滤器的doFilter的方法*/

    public void doFilter(ServletRequest req, ServletResponse res,

            FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;

        HttpServletResponse response = (HttpServletResponse) res;

        //获取访问的连接地址

        String path = request.getServletPath();

        //在访问首页index.jsp页面之前,先从Cookie中获取namepassword的值,并显示在页面上(完成记住我)

        this.forwordIndexPage(path,request);

        //放行

        chain.doFilter(request, response);

    }

    /**销毁*/

    public void destroy() {

 

    }

    /**在访问首页index.jsp页面之前,先从Cookie中获取name,password的值,并显示在页面上(完成记住我)*/

    private void forwordIndexPage(String path, HttpServletRequest request) {

        if(path!=null && path.equals("/index.jsp")){

            String name = "";

            String password = "";

            String checked = "";

            Cookie [] cookies = request.getCookies();

            if(cookies!=null && cookies.length>0){

                for(Cookie cookie:cookies){

                    if(cookie.getName().equals("name")){

                        name = cookie.getValue();

                        /**

                         * 如果name出现中文,对中文进行解码

                         */

                        try {

                            name = URLDecoder.decode(name, "UTF-8");

                        } catch (UnsupportedEncodingException e) {

                            e.printStackTrace();

                        }

                        checked = "checked";

                    }

                    if(cookie.getName().equals("password")){

                        password = cookie.getValue();

                    }

                }

            }

            request.setAttribute("name", name);

            request.setAttribute("password", password);

            request.setAttribute("checked", checked);

        }

    }

}

 

第五步:在web.xml中添加:

<!-- 自定义过滤器,要求添加到struts2过滤器的前面 -->

<filter>

    <filter-name>SystemFilter</filter-name>     <filter-class>cn.itcast.elec.util.SystemFilter</filter-class>

</filter>

<filter-mapping>

    <filter-name>SystemFilter</filter-name>

    <url-pattern>*.do</url-pattern>

    <url-pattern>*.jsp</url-pattern>

</filter-mapping>


测试后:发现问题:如果name中存在中文,此时中文字符是不能存放到Cookie对象中,使用HttpResponse对象添加Cookie会抛出异常。

解决方案:使用URLEncode类和URLDecode类进行编码和解码

在LogonUtils类对name进行编码:

try {

    name = URLEncoder.encode(name, "UTF-8");

} catch (UnsupportedEncodingException e) {

    e.printStackTrace();

}

//1:创建2个Cookie,存放指定值

Cookie nameCookie = new Cookie("name",name);

Cookie passwordCookie = new Cookie("password",password);

在过滤器SystemFilter类对name进行解码:

if(cookie.getName().equals("name")){

    name = cookie.getValue();

    /**

    * 如果name出现中文,对中文进行解码

    */

    try {

        name = URLDecoder.decode(name, "UTF-8");

    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();

    }

    checked = "checked";

}

posted @ 2016-07-19 12:52  kimi9py  阅读(148)  评论(0编辑  收藏  举报