Jsp开发中使用Cookie实例(实现记住密码功能)

Login.java

/******************************************************/

注意:由于在jsp中创建Cookie时,必须保证Cookie创建语句写在jsp页面头部的<jsp:include page="includes/head.jsp" flush="true" />标签之前才会生效,否则手动添加的Cookie失败。

所以我决定讲创建Cookie语句放在servlet内。

             String ischeck = request.getParameter("isMemPassword ");

             try {
                        if(ischeck.equals("on")){
                            //表示用户点击了【记住密码】按钮
                            //创建cookie
                            Cookie user = new Cookie("user", username + "-" + password);
                            user.setMaxAge(60*60*24);
                            response.addCookie(user);

                        }else{
                            Cookie deleteNewCookie=new Cookie("user",null);
                            deleteNewCookie.setMaxAge(0); //删除该Cookie
                            deleteNewCookie.setPath("/");
                            response.addCookie(deleteNewCookie);

                        }
                       
                        Authentication.login(username, password, request,
                                response);
                    } catch (Exception le) {
                        request.setAttribute("loginErrorMessage3", new String(
                                "true"));
                        return false;
                    }

 

 

login.jsp

//-------------------------------------------------------------------------------------

注意:那么在jsp页面中对已经创建好的Cookie读取,成功。

至此,可以实现【记住密码】功能。

<%
    try {
        Cookie[] cookies = request.getCookies() ;
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie newCookie = cookies[i];
               
                if ("user".equals(newCookie.getName() )) {
                    String name = newCookie.getValue().split("-")[0];
                    String passward = newCookie.getValue() .split("-")[1];
                    request.setAttribute("name ", name);
                    request.setAttribute("pass ", passward);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
%>

<form name="frmAction" method="post" id="backingFileLoginForm" action="<%=url.toString()%>" type="POST">
    <table border="0" width="100%" >
        <tr>
            <td align="center" colspan="2">用户名: <input type="text" size=15 name="username" value="${name } "></td>
            <td>
        </tr>
        <tr>
              <td align="center" colspan="2">密码:    <input type="password" size=15 name="password" value="${pass } "></td>
        </tr>
        <tr>
              <td align="center" colspan="2"><input type="checkbox "  name="isMemPassword ">记住密码</td>
        </tr>
        <tr>
              <td colspan="2" align="center">
                  <input type="submit" name="send" value="门户登录">
              </td>
        </tr>
    </table>
</form>

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值