隨筆-51  評論-14  文章-0  trackbacks-0
          附加功能:登錄成功后保存Cookie一段時間,在這期間無需重新登錄
          一個基本的登錄模塊至少分為4個頁面:
          1.輸入用戶信息頁面(login.jsp);
          2.用戶合法性驗證頁面(check.jsp);
          3.登錄成功歡迎頁面(pass.jsp);
          4.登錄失敗提示頁面(failure.jsp)。

          為了實現保存Cookie功能,還需增加一個頁面:
          5.檢查Cookie頁面(index.jsp)

          結構圖如下:
          ---------------------------------------------------------------------

                           index.jsp
                                 |
                                 |判斷Cookie中有無用戶名、密碼
                    ----------------------
                    |   Y                 N  |
                    |                          V
                    |                      login.jsp<--------------------
                    |                          |輸入用戶名、密碼           |
                    |                          V                                  |
                    ---------------->check.jsp                           |
                                                |                                  |
                                                |驗證用戶名、密碼          |
                                    ---------------------                     |
                                    |  Y                N  |                     |
                                    V                       V                    |
                                pass.jsp           failure.jsp----------

          ---------------------------------------------------------------------

          index.jsp:
          <%@ page contentType="text/html;charset=GB2312" %>
          <html>
            
          <head>
              
          <title>index</title>
            
          </head>
            
            
          <body>
              
          <%
                  
          int i;
                  
          //初始化,用于保存Cookie中的用戶名、密碼
                  String C_username="";
                  String C_password
          ="";
                  
          //獲取全部Cookie
                  Cookie c[]=request.getCookies();
                  
          for(i=0;i<c.length;i++)
                  {
                      
          //在Cookie中查找用戶名、密碼,如果找到,則分別將其賦值給用戶名、密碼變量
                      if("username".equals(c[i].getName()))
                          C_username
          =c[i].getValue();
                      
          if("password".equals(c[i].getName()))
                          C_password
          =c[i].getValue();
                  }
                  
          if(!"".equals(C_username) && !"".equals(C_password))
                  {
                      
          //Cookie中有用戶名、密碼,將用戶名、密碼提交到驗證頁面
                      response.sendRedirect("check.jsp?username="+C_username+"&password="+C_password);
                  }
                  
          else
                  {
                  
          //Cookie中沒有用戶名、密碼,跳轉到登錄頁面
              %>
                  
          <jsp:forward page="login.jsp" />
              
          <%
                  }
              
          %>
            
          </body>
          </html>

          login.jsp:

          <%@ page contentType="text/html;charset=GB2312" %>
          <html>
            
          <head>
              
          <title>登錄</title>
            
          </head>
            
            
          <body>
                
          <center>
                
          <h1>登錄頁面</h1>
                
          <hr>
              
          <form action="check.jsp" method="post">
                  
          <table>
                      
          <tr>
                          
          <td>用戶名:</td>
                          
          <td><input type="text" name="username" /></td>
                      
          </tr>
                      
          <tr>
                          
          <td>密  碼:</td>
                          
          <td><input type="password" name="password" /></td>
                      
          </tr>
                      
          <tr>
                      
          <td>Cookie選項:</td>
                      
          <td>
                          
          <input type="radio" name="cookie" value="nosave" checked>不保存
                          
          <input type="radio" name="cookie" value="save">保存1分鐘
                      
          </td>
                      
          </tr>
                      
          <tr>
                          
          <td colspan="2" align="center">
                              
          <input type="submit" value="登錄" /> 
                              
          <input type="reset" value="重置" />
                          
          </td>
                      
          </tr>
                  
          </table>
              
          </form>
              
          </center>
            
          </body>
          </html>

           

          check.jsp:

          <%@ page contentType="text/html;charset=GB2312" %>
          <html>
            
          <head>
              
          <title>驗證頁面</title>
            
          </head>
            
            
          <body>
              
          <%
                  String Username
          =request.getParameter("username");
                  String Password
          =request.getParameter("password");
                  String IsCookie
          =request.getParameter("cookie");
                  
          //判斷用戶名、密碼的合法性
                  if("magci".equals(Username) && "123456".equals(Password))
                  
          //為了避免空指向異常,比較兩個字符串時,如有字符串常量,則使用字符串常量的“equals”方法(即將常量寫在前面)。
                  {
                      
          //合法用戶
                      if("save".equals(IsCookie))
                      {
                          
          //如果選擇了保存Cookie選項,則保存Cookie
                          Cookie c1=new Cookie("username",Username);
                          Cookie c2
          =new Cookie("password",Password);
                          
          //設置Cookie保存時間為1分鐘
                          c1.setMaxAge(60);
                          c2.setMaxAge(
          60);
                          response.addCookie(c1);
                          response.addCookie(c2);
                      }
                      
          //跳轉到歡迎頁面
                      %>
                      
          <jsp:forward page="pass.jsp"/>
                      
          <%
                  }
                  
          else
                  {
                      
          //非法用戶,跳轉到登錄失敗頁面
                      %>
                      
          <jsp:forward page="failure.jsp" />
                      
          <%
                  }
              
          %>
            
          </body>
          </html>


          pass.jsp:

          <%@page contentType="text/html;charset=GB2312" %>
          <center>
              
          <h1>登錄成功!!</h1>
              
          <hr>
              
          <h3>歡迎<font size="12" color="red">
              
          <%--forward跳轉為服務器端跳轉,跳轉后仍在check.jsp頁面,可以繼續使用usename參數 --%>
              
          <%=request.getParameter("username"%>
              
          </font>光臨!</h3>
          </center>


          failure.jsp:

          <%@ page contentType="text/html;charset=GB2312" %>
          <div align="center">
          <h1>登錄失敗!!</h1>
          <hr>
          <a href="login.jsp">重新登錄</a>
          </div>

          posted on 2008-04-21 20:11 Hank1026 閱讀(5203) 評論(3)  編輯  收藏

          評論:
          # re: 使用COOKIE登錄驗證(轉載) 2012-03-21 09:48 | ghg
          gfhgfhgf  回復  更多評論
            
          # re: 使用COOKIE登錄驗證asd(轉載) 2014-05-13 19:47 | asdas
          asdasd  回復  更多評論
            
          # re: 使用COOKIE登錄驗證(轉載) 2016-01-04 15:11 | vfdgv
          fsfsdvsdfv  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 广西| 进贤县| 招远市| 白玉县| 三都| 邹城市| 邯郸市| 临朐县| 涪陵区| 富源县| 井冈山市| 万载县| 平陆县| 青铜峡市| 澎湖县| 日土县| 会昌县| 丰镇市| 天全县| 临泉县| 房产| 临沭县| 出国| 余姚市| 屯昌县| 甘德县| 庐江县| 南雄市| 德钦县| 大连市| 桐城市| 西畴县| 民和| 辛集市| 定远县| 庆元县| 全南县| 平阳县| 新巴尔虎左旗| 九江县| 普陀区|