• 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="_c3F041F78-53B9-69A5-3B3C-01BAB6E90925_k37408022-0986-F9AF-9DBB-8FBEC82968E9" />
        <input type="hidden" name="_eventId" value="submit" />
     
        <input class="button"   name="submit" accesskey="l" value="登 录" 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=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); } }
  • 相关阅读:
    高效 JavaScript
    什么是QName【转】
    gson的简单使用方法
    SWT的FormLayout
    SWT/JFace常用组件容器类
    更改swing应用程序标题栏默认图标
    面试也是自己对自己的面试
    关于Android图片cache处理方法
    【Java】_2_Java程序入门第二课
    【算法和数据结构】_9_线性结构_队列_续_1
  • 原文地址:https://www.cnblogs.com/kentyouyou/p/3729703.html
Copyright © 2020-2023  润新知