qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Java web項目用cookie記住用戶名、密碼

           web 項目中一般在登錄的時候都會用到記住密碼功能。
            1.jsp頁面:
          <% String flag = (String)session.getAttribute("flag")==null?"":(String)session.getAttribute("flag");
          String name = "";
          String password = "";
          try{
          Cookie[] cookies=request.getCookies();
          if(cookies!=null){
          for(int i=0;i<cookies.length;i++){
          if(cookies[i].getName().equals("cookie_user")){
          String value =  cookies[i].getValue();
          if(value!=null&&!"".equals(value)){
          name=cookies[i].getValue().split("-")[0];
          if(cookies[i].getValue().split("-")[1]!=null && !cookies[i].getValue().split("-")[1].equals("null")){
          password=cookies[i].getValue().split("-")[1];
          }
          }
          }
          request.setAttribute("name",name);
          request.setAttribute("passward",password);
          }
          }
          }catch(Exception e){
          e.printStackTrace();
          }
          %>
          <body>
          <div id="logo"><img src="<%=request.getContextPath() %>/frontsite/Images/Logo.jpg" width="244" height="44" alt="" /></div>
          <div id="loginfrm">
          <form action="<%=request.getContextPath() %>/frontsite/login.do?method=login" method="post" id="forms" onsubmit="checkForm();return false;">
          <div class="frm">
          <ul>
          <li class="l1">用戶名:</li>
          <li class="l2"><input name="login_id" type="text" id="login_id" class="input" value="<%=name %>"/></li>
          <li class="l3">* 最大限度為20字節</li>
          <li class="l1">密碼:</li>
          <li class="l2"><input name="login_pwd" type="password" id="login_pwd" class="input" value="<%=password %>"/></li>
          <li class="l3">* 最大限度為20字節</li>
          <li class="l1">驗證碼:
          <li class="l2" style="width: 180px;"><input name="checkImg" id="checkImg" type="text" class="input" style="width: 60px;" size="10" />
          &nbsp;&nbsp;<img src="<%=request.getContextPath() %>/CheckImg_FT" width="49" height="22" /></li>
          <li class="l4">
          <label><input type="checkbox" name="flag" id="flag" value="1" <%if(flag!=null && flag.equals("1")){%> checked ; value ="1"; <%}else {%> value="0" <%;}%> />記住密碼</label>
          <label><span style="margin-left:10px; color: #F00;" ><html:errors /></span></label>
          </li>
          <li class="l4"><input type="image" name="imageField" id="imageField" src="<%=request.getContextPath() %>/frontsite/Images/login_bnt.jpg" /></li>
          </ul>
          </div>
          </form>
          </div>
          </body>


            2.java 類:
          String flag = request.getParameter("flag");
          //set cookie
          if(flag!=null && flag.equals("1")){
          Cookie cookie = new Cookie("cookie_user", po.getLogin_id()+"-"+form.getLogin_pwd());
          cookie.setMaxAge(60*60*24*30); //cookie 保存30天
          response.addCookie(cookie);
          }else{
          Cookie cookie = new Cookie("cookie_user",po.getLogin_id()+"-"+null);
          cookie.setMaxAge(60*60*24*30); //cookie 保存30天
          response.addCookie(cookie);
          }

          posted on 2013-11-12 11:02 順其自然EVO 閱讀(2304) 評論(0)  編輯  收藏 所屬分類: android

          <2013年11月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 宜章县| 木兰县| 海门市| 赞皇县| 扎兰屯市| 喀喇沁旗| 长子县| 丹棱县| 阿巴嘎旗| 凤台县| 聂拉木县| 清水河县| 新蔡县| 沙雅县| 广宗县| 鸡西市| 台北市| 襄垣县| 沙河市| 阿荣旗| 台南县| 通江县| 丰城市| 乌兰浩特市| 大丰市| 辉南县| 东丽区| 剑河县| 聊城市| 青浦区| 青海省| 鹤峰县| 新绛县| 正阳县| 兴海县| 阿城市| 双桥区| 井陉县| 蛟河市| 桑植县| 祁东县|