我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數據加載中……

          Java對Cookie的操作

          public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
                  LazyValidatorForm userForm 
          = (LazyValidatorForm) form;
                  log.info(
          "UserAction:驗證用戶登錄");
                  
          //得到用戶名和密碼
                  String username = (String) userForm.get("username");
                  String password 
          = (String) userForm.get("password");
                  Map
          <String,Object> userMap = new HashMap<String,Object>();
                  userMap.put(
          "userName", username);
                  userMap.put(
          "userPassword", password);
                  
          //設置Cookie
                  int cookieDate = Integer.parseInt(userForm.get("cookieDate").toString());
                  
          if(cookieDate != 0)
                  
          {
                      Cookie cookie1 
          = new Cookie("username",username);
                      Cookie cookie2 
          = new Cookie("password",password);
                      
          if(cookieDate == 1)
                      
          {
                          
          //一天
                          cookie1.setMaxAge(60 * 60 * 24);
                          cookie2.setMaxAge(
          60 * 60 * 24);
                      }

                      
          if(cookieDate == 2)
                      
          {
                          
          //一個月
                          cookie1.setMaxAge(60 * 60 * 24 * 31);
                          cookie2.setMaxAge(
          60 * 60 * 24 * 31);
                      }

                      
          if(cookieDate == 3)
                      
          {
                          
          //一年
                          cookie1.setMaxAge(60 * 60 * 24 * 365);
                          cookie2.setMaxAge(
          60 * 60 * 24 * 365);
                      }

                      
          //把捕獲來的cookies添加到響應里
                      response.addCookie(cookie1);
                      response.addCookie(cookie2);
                  }

                  User user 
          = null
                  
          //驗證用戶名和密碼
                  if(userManager.createCriteria(Restrictions.allEq(userMap)).list().size() > 0)
                  
          {
                      log.info(
          "UserAction:登錄成功");
                      user 
          = (User) userManager.createCriteria(Restrictions.allEq(userMap)).list().get(0);
                      request.getSession().setAttribute(
          "user", user);
                      
          return mapping.findForward("success");
                  }

                  
          else
                  
          {
                      
          this.saveMessage(request, "login.error""用戶名或密碼錯誤");
                      log.info(
          "UserAction:登錄失敗");
                      request.setAttribute(
          "error","username or password is wrong");
                      
          return mapping.getInputForward();
                  }

                  
              }
          登錄前的跳轉:
          /**
               * 登錄前的跳轉
               * 
          @param mapping
               * 
          @param form
               * 
          @param request
               * 
          @param response
               * 
          @return
               
          */

              
          public ActionForward forwardlogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
                  log.info(
          "UserAction:登錄前的跳轉,Cookie處理");
                  
          //取出cookies
                  Cookie[] cookies = request.getCookies();
                  String username 
          = null;
                  String password 
          = null;
                  
          if(cookies.length != 0)
                  
          {
                      
          for(int i = 0; i < cookies.length; i ++)
                      
          {
                          
          //根據key取出值
                          if(cookies[i].getName().equals("username"))
                          
          {
                              username 
          = cookies[i].getValue();
                          }

                          
          if(cookies[i].getName().equals("password"))
                          
          {
                              password 
          = cookies[i].getValue();
                          }

                      }

                  }

                  request.setAttribute(
          "username", username);
                  request.setAttribute(
          "password", password);
                  
          return mapping.findForward("login");
              }

          前臺:
          <form action="${ctx}/user.do?method=login" method="post" onsubmit="return check();">
                          
          <table cellpadding=0 cellspacing=0 border=0 width=98%
                              
          bgcolor=#0099cc align=center>
                              
          <tr>
                                  
          <td>
                                      
          <table cellpadding=6 cellspacing=1 border=0 width=100%>

                                          
          <tr>
                                              
          <td bgcolor=#99ccff valign=middle colspan=2 align=center>
                                                  
          <!-- 判斷用戶名或密碼是否錯誤 -->
                                                  
          <logic:empty name="error" scope="request">
                                                      
          <font color="green"><b>請輸入您的用戶名、密碼登陸</b></font>
                                                  
          </logic:empty>
                                                  
          <logic:notEmpty name="error" scope="request">
                                                      
          <bean:message key="login.error"/>
                                                  
          </logic:notEmpty>
                                              
          </td>
                                          
          </tr>
                                          
          <tr>
                                              
          <td bgcolor=#f2f8ff valign=middle>
                                                  請輸入您的用戶名
                                              
          </td>
                                              
          <td bgcolor=#f2f8ff valign=middle>
                                                  
          <INPUT value="${requestScope.username}" name="username" type=text id="username" onblur="return check();">
                                                  
          &nbsp;
                                                  
          <href="${ctx}/main/common/reg.jsp">沒有注冊?</a>
                                              
          </td>
                                              
          <!-- 用戶名錯誤提示信息 -->
                                              
          <td id="username_info" style="color:red;position: absolute;left:550px;top:235px;"></td>
                                          
          </tr>
                                          
          <tr>
                                              
          <td bgcolor=#f2f8ff valign=middle>
                                                  請輸入您的密碼
                                              
          </td>
                                              
          <td bgcolor=#f2f8ff valign=middle>
                                                  
          <INPUT value="${requestScope.password}" name="password" type=password id="password" onblur="return check();">
                                                  
          &nbsp;
                                                  
          <href="lostpass.jsp">忘記密碼?</a>
                                              
          </td>
                                              
          <!-- 密碼錯誤提示信息 -->
                                              
          <td id="password_info" style="color:red;position: absolute;left:550px;top:270px;"></td>
                                          
          </tr>
                                          
          <tr>
                                              
          <td bgcolor=#f2f8ff valign=top width=30%>
                                                  
          <b>Cookie 選項</b>
                                                  
          <BR>
                                                  請選擇您的Cookie保存時間
          <br>
                                                  下次訪問可以方便輸入
          <br><br />
                                                  
          <href="#" onclick="clearCookie();" style="color:green;">清空Cookies</a>
                                                  
          <div id="cookie_info" style="color:maroon;position: absolute;left:100px;top:360px;"></div>
                                              
          </td>
                                              
          <td bgcolor=#f2f8ff valign=middle>
                                                  
          <input type="radio" id="cookieDate" name="cookieDate" value="0" checked>
                                                  不保存,關閉瀏覽器就失效
                                                  
          <br>
                                                  
          <input type="radio" id="cookieDate"  name="cookieDate" value="1">
                                                  保存一天
                                                  
          <br>
                                                  
          <input type="radio" id="cookieDate"  name="cookieDate" value="2">
                                                  保存一月
                                                  
          <br>
                                                  
          <input type="radio" id="cookieDate" name="cookieDate" value="3">
                                                  保存一年
                                                  
          <br>
                                              
          </td>
                                          
          </tr>
                                          
          <input type=hidden name=comeURL
                                              
          value="#" />
                                          
          <tr>
                                              
          <td bgcolor=#99ccff valign=middle colspan=2 align=center>
                                                  
          <input type=submit name="submit" value="登 陸">
                                              
          </td>
                                          
          </tr>
                                      
          </table>
                                  
          </td>
                              
          </tr>
                          
          </table>
                      
          </form>


          posted on 2008-01-23 18:16 々上善若水々 閱讀(2729) 評論(0)  編輯  收藏 所屬分類: JavaWeb

          主站蜘蛛池模板: 永平县| 邳州市| 太和县| 霍城县| 土默特左旗| 无为县| 巴南区| 金山区| 珠海市| 玉溪市| 台南市| 封丘县| 莱阳市| 隆安县| 武鸣县| 开封市| 沛县| 古交市| 巨鹿县| 溧水县| 康定县| 桃园县| 钦州市| 漯河市| 娱乐| 罗山县| 青铜峡市| 涟水县| 聂拉木县| 扬中市| 安塞县| 会同县| 阜新市| 高青县| 綦江县| 福贡县| 乐业县| 高密市| 曲周县| 宁化县| 尼木县|