首页 > 代码库 > freeecms项目中cookies的使用

freeecms项目中cookies的使用

cookie.js 代码:

function GetCookieVal(offset)
//获得Cookie解码后的值
{
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));//unescape解码
}


function SetCookie(name, value)
//设定Cookie值
{
var expdate = new Date();
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))//escape编码
+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
+((secure == true) ? "; secure" : "");
}


function DelCookie(name)
//删除Cookie
{
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}


function GetCookie(name)
//获得Cookie的原始值
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return GetCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
} 

 

login.jsp中代码:

<div class="input_post">
    <input type="hidden" name="lt" value="http://www.mamicode.com/_c3F041F78-53B9-69A5-3B3C-01BAB6E90925_k37408022-0986-F9AF-9DBB-8FBEC82968E9" />
    <input type="hidden" name="_eventId" value="http://www.mamicode.com/submit" />
 
    <input class="button"   name="submit" accesskey="l" value="http://www.mamicode.com/登 录" tabindex="4" type="submit" />
</div> <div class="input_fpwd"><input id="RememberMe" type="checkbox" name="RememberMe" onclick="RememberMeClick()"/>
  <label for="RememberMe">在此计算机上保留登录帐号</label>
</div>

 

login.jsp中js代码:

 <script>
            if(GetCookie("FreeCMS_loginName")!=null&&GetCookie("FreeCMS_loginName")!=""){
                document.getElementById("RememberMe").checked=true;
            }
            if(GetCookie("FreeCMS_loginName")!=null)document.getElementById("username").value=http://www.mamicode.com/GetCookie("FreeCMS_loginName");
            function RememberMeClick(){
                if(document.getElementById("RememberMe").checked==false){
                    //删除cookie
                    DelCookie("FreeCMS_loginName");
                }
            }
            function submitForm(){
                
                if($.trim($("#username").val())==""){
                    alert("请输入用户名!");
                    $("#username").focus();
                    return false;
                }
                if($.trim($("#password").val())==""){
                    alert("请输入密码!");
                    $("#password").focus();
                    return false;
                }
                if($.trim($("#ValidateCode").val())==""){
                    alert("请输入验证码!");
                    $("#ValidateCode").focus();
                    return false;
                }
                if(document.getElementById("RememberMe").checked==true){
                    //设置cookie
                    SetCookie("FreeCMS_loginName",document.getElementById("username").value,60*60*24*365);
                }
                return true;
            }
            $("#username").focus();
            </script>


service中代码:

import javax.servlet.http.Cookie;

public
String login(){ try { //记住用户名 if("on".equals(RememberMe)){ Cookie cookie=new Cookie("FreeCMS_memberLoginName",EscapeUnescape.escape(member.getLoginname())); cookie.setMaxAge(1000*60*60*24*365);//有效时间为一年 getHttpResponse().addCookie(cookie); } HttpSession session =getHttpSession(); if (ValidateCode!=null && ValidateCode.equals(session.getAttribute("rand"))) { showMessage=memberService.checkLogin(getHttpSession(), member); }else { showMessage="验证码错误!"; } if (showMessage==null || "".equals(showMessage)) { OperLogUtil.log(member.getLoginname(), "会员登录", getHttpRequest()); getHttpResponse().sendRedirect("member/member_index.do"); return null; }else { return showMessage(showMessage, forwardUrl, forwardSeconds); } } catch (Exception e) { DBProException(e); OperLogUtil.log(member.getLoginname(), "会员登录失败:"+e.toString(), getHttpRequest()); return showMessage("出现错误:"+e.toString()+"", forwardUrl, forwardSeconds); } }