对Cookie操作如登录时记住用户名及密码

<%@page contentType="text/html;charset=utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户登录</title>
<style type="text/css">
.login{
      font-family:宋体;
   font-size:12px;
}
.login td{
 height:30px;
}


.align{font-size:12px;}
.align input{display:block;}
.align span{display:block; margin-left:22px!important; margin-left:11px; margin-top:-16px; float:left; height:16px; line-height:14px;}

</style>
</head>

<script type="text/javascript">
 document.onkeydown=ShortcutKey;
  function ShortcutKey(){
 if(window.event.keyCode==13) {
  submitForm();
  }
 }

   function submitForm() {
    if (document.getElementById("optorId").value == "") {
     alert("用户名必须填写!");
     document.getElementById("optorId").focus();
     return false;
    } else {
     if(document.getElementById('saveLoginPwd').checked==true){
      setCookie("officeLoginName",document.getElementById('optorId').value);
      setCookie("officeLoginType",document.loginForm.loginType.value);
      setCookie("officeLoginPwd",document.getElementById('pwd').value);
     }else if(document.getElementById('saveLoginName').checked==true){
      setCookie("officeLoginName",document.getElementById('optorId').value);
      setCookie("officeLoginType",document.loginForm.loginType.value);
      delCookie("officeLoginPwd");
     }else{
      delCookie("officeLoginName");
      delCookie("officeLoginType");
      delCookie("officeLoginPwd");
     }
     document.loginForm.submit();
    }
   }
   function loadWindow() {
    if(document.getElementById('optorId').value==""){
     var officeLoginName=getCookie("officeLoginName");
     var officeLoginType=getCookie("officeLoginType");
     var officeLoginPwd=getCookie("officeLoginPwd");
     if(officeLoginName!=null&&officeLoginName!=""&&officeLoginName!="null"){
      document.getElementById('optorId').value=officeLoginName;
      document.getElementById('saveLoginName').checked=true
     }
     if(officeLoginType!=null&&officeLoginType!=""&&officeLoginType!="null"){
      document.loginForm.loginType.value=officeLoginType;
     }
     if(officeLoginPwd!=null&&officeLoginPwd!=""&&officeLoginPwd!="null"){
      document.getElementById('pwd').value=officeLoginPwd;
      document.getElementById('saveLoginPwd').checked=true
     }
    }
    document.loginForm.elements["optorId"].focus();
   }
   function setCookie(name,value) {
       var exp  = new Date("December 31, 9998");
       document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
   }
   function getCookie(name){
       var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
       if(arr != null) return unescape(arr[2]); return null;
   } 
   function delCookie(name){
       var exp = new Date();
       exp.setTime(exp.getTime() - 1);
       var cval=getCookie(name);
       if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
   }
   
   window.onload=loadWindow;
  </script>


<body style="background-color:#C0C0C0; padding:0;margin:0;">
       <div style="margin:0 auto; width:1002px;position: relative;"><img src="<%=com.officeFrame.app.App.webUrl%>/appfiles/aicu/imgs/bj.jpg" border="0">
          <div style="width:300px; height:160px;position: absolute;top:220px;left:580px; ">
     <form action="<%=com.officeFrame.app.App.webUrl%>/optorLogin_login.so" method="post" name="loginForm" class="loginform" onsubmit="submitForm();">
     <input type="hidden"  id="returnUrl" name="returnUrl" value="<%=WebUtils.getParameter(request, "returnUrl")%>" />
     
     <table class="align login" width="100%" >
      <tr>
         <input type="hidden" name="loginType" value="01">
       <td width="25%" align="right">用户名:</td>
       <td width="40%" ><input type="text"  style="width:130px;height:14px;" id="optorId" name="optorId" maxlength="30" value="<%=WebUtils.getParameter(request, "optorId")%>" /></td>
       <td width="35%"align="left"><input type="checkbox" name="saveLoginName" id="saveLoginName" value="1" /><span>记住登录名</span></td>
      </tr>
      <tr>
       <td align="right">密&nbsp;&nbsp;码:</td>
       <td><input type="password" style="width:130px;height:14px;" name="pwd" id="pwd" maxlength="20" /></td>
       <td><input type="checkbox" name="saveLoginPwd" id="saveLoginPwd" value="1" /><span>记住密码</span></td>
      </tr>
      <tr>
          <td style="height:10px;"></td>
      </tr>
      <tr>
       <td colspan="3" align="center">
        <img onclick="submitForm();" src="<%=com.officeFrame.app.App.webUrl%>/appfiles/aicu/imgs/dl1.jpg" border="0">
        <img onclick="loginForm.reset();" src="<%=com.officeFrame.app.App.webUrl%>/appfiles/aicu/imgs/cz1.jpg" border="0">
       </td>
      </tr>
     </table>
      </form>
    </div>
    <div style="color:#FFFFFF;font-size:12px;position: absolute;top:530px;left:370px;">亚信联创科技(中国)有限公司 版权所有 法律公告</div>
    </div>
</body>
</html>

猜你喜欢

转载自jiangjingxiaoxuan.iteye.com/blog/1195928