网页登陆页面设置Cookie

cookie是保存http协议状态的一个重要手段,基本上所有网站都会使用到cookie机制,cookie是session的一个实现方式,当然现在还有token来代替cookie,但cookie依旧是很重要的一个模式,这里在js中实现设置cookie进而实现登陆访问。

<%@ page contentType="text/html; charset=GB2312"%>
<%@ page import="tellhow.commonweb.system.*" %>
<%
   String useraccounts="";
   String userpassword="";
   String Messageinfo="请输入用户信息";
   useraccounts=request.getParameter("accounts");
   userpassword=request.getParameter("password");
   String flag = request.getParameter("flag")==null?"":request.getParameter("flag");//flag为1则为从oms系统登陆 
   if(useraccounts!=null&&userpassword!=null){
   if(!useraccounts.equals("null")&&!useraccounts.equals("")&&!userpassword.equals("null")&&!userpassword.equals("")){
   loginBean myloginbean=new loginBean();
   myloginbean.init(pageContext);
   int retval;
   retval=myloginbean.checkuserlogin(useraccounts,userpassword,flag);
   switch(retval)
   {
      case 1:
         // response.sendRedirect("system/index_manage.jsp");
         // response.sendRedirect("gz/index.jsp");
         String url = request.getContextPath() + "/getUserMsg.do?method=getUserInfo&username=" + useraccounts;
         System.out.println(url);
         response.sendRedirect(url);
      break;
      case -1:
          Messageinfo="您输入的密码不对,请重新输入!";
      break;
      case -2:
          Messageinfo="您输入的用户不存在,请重新输入!";
      break;
   }
   }
 }
%>
<script language="javascript" type="text/javascript">
    function window_Onload(){
      loginfrm.accounts.focus();
    }
    function onInputKeyup(){
      if(event.keyCode==13){
              if(event.srcElement.name=="accounts"){
                  loginfrm.password.focus();
                  return;
            }
            if(event.srcElement.name=="password"){
                  loginfrm.submit();
                  return;
            }
        }
    }
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>网站管理系统</title>
<!--[if lte IE 6]>
<script src="../css/mycss/js/DD_belatedPNG_0.0.8a.js" type="text/javascript"></script>
<script type="text/javascript">
    DD_belatedPNG.fix('*');
</script>
<![endif]-->
<link href="css/mycss/style/public.css" type="text/css" rel="stylesheet" />
<script src="css/mycss/js/jquery.js" type="text/javascript"></script>
<script src="css/mycss/js/index.js" type="text/javascript"></script>
<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="js/jquery.cookie.js" type="text/javascript"></script>
</head>
<body  onload="window_Onload();getCookie();">
<FORM action=login.jsp method=post name="loginfrm">
<div class="main_area">
    <div class="main_left_area"><img src="css/mycss/images/logo_bg01.png" width="280" height="130" /></div>
    <div class="main_mid_area"></div>
    <div class="main_right_area"></div>
    <div class="main_info_area">
        <div class="fill01_area"></div>
        <div class="title_area"><img src="css/mycss/images/main_bg02.png" width="487" height="46" alt="网站管理系统"/></div>
        <div class="fill02_area"></div>
        <div class="login_area">
            <div class="login_left_area">
                <div class="fill03_area"></div>
                <div class="login_info_area">
                    <span>用户名:</span><input name="accounts" id="accounts" type="text" onkeyup="onInputKeyup();">
                    <div class="fill04_area"></div>
                    <span>密 码:</span><input name="password" id="password" type="password" onkeyup="onInputKeyup();">
                </div>
            </div>
            <div class="login_right_area">
                <a  onclick="savedata();"></a>
            </div>
            <!-- <a class="href03_area" href="/login.jsp?accounts=guest&password=1"><span ></span><font face="verdana" size="3" color="black"> 来宾访问</font></a> -->
        </div>
        <div class="login_href_area">
            <input type="checkbox" id="boxchange" name="checkbox" value="1"/>
            <a>记住用户名密码</a>
            <a class="href03_area" href="/login.jsp?accounts=guest&password=1"></a>
        </div>
        <div class="flag_area"><%=Messageinfo%></div>
        <div ></div>
        <div ></div>
        <div class="login_href_areabottom"><font >调控中心用户请使用OMS账户登录,其他用户请点击“来宾访问”</font></div>
        <div class="foot_area">广州供电局有限公司 版权所有  技术支持:泰豪软件</div>
    </div>
</div>
</form>
</body>
<script type="text/javascript">
    var checkstate = null;//标识符,是为了在保存前调用设置cookie方法的进行判断
    $("#boxchange").change(function() { 
        if(document.all.checkbox.checked){ 
             checkstate = "1";
         }else{
             checkstate = "0";
        }
         
    });
    function savedata(){
        //存储cookie
          if("1" == checkstate){  
                setCookie();
                //$.cookie("pwd","");
        }else if("0" == checkstate){
            $.cookie('login_code', null, { expires: -1 });
              $.cookie('pwd', null, { expires: -1 });
        }
        loginfrm.submit();
    }
        
    //设置cookie
    function setCookie(){ //设置cookie    
       var loginCode = $("#accounts").val(); //获取用户名信息    
       var pwd = $("#password").val(); //获取登陆密码信息    
       $.cookie('login_code',loginCode, { expires: 7 });//调用jquery.cookie.js中的方法设置cookie中的用户名    
       $.cookie('pwd',pwd, { expires: 7 });//调用jquery.cookie.js中的方法设置cookie中的登陆密码
    }   
    function getCookie(){ //获取cookie    
        var loginCode = $.cookie('login_code'); //获取cookie中的用户名    
        var pwd =  $.cookie('pwd'); //获取cookie中的登陆密码    
        if(loginCode){//用户名存在的话把用户名填充到用户名文本框    
           $("#accounts").val(loginCode);    
        }    
        if(pwd){//密码存在的话把密码填充到密码文本框    
           $("#password").val(pwd);   
        } 
      //选中保存秘密的复选框
        if(loginCode != null && loginCode != '' && pwd != null && pwd != ''){
        $("[name='checkbox']").attr("checked","true");   
              checkstate = "1";
         }else{
             checkstate = "0";
      }
 }     

</script>
</html>
Last modification:November 25th, 2019 at 05:09 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment