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

          為了實(shí)現(xiàn)保存Cookie功能,還需增加一個(gè)頁(yè)面:
          5.檢查Cookie頁(yè)面(index.jsp)

          結(jié)構(gòu)圖如下:
          ---------------------------------------------------------------------

                           index.jsp
                                 |
                                 |判斷Cookie中有無(wú)用戶(hù)名、密碼
                    ----------------------
                    |   Y                 N  |
                    |                          V
                    |                      login.jsp<--------------------
                    |                          |輸入用戶(hù)名、密碼           |
                    |                          V                                  |
                    ---------------->check.jsp                           |
                                                |                                  |
                                                |驗(yàn)證用戶(hù)名、密碼          |
                                    ---------------------                     |
                                    |  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中的用戶(hù)名、密碼
                  String C_username="";
                  String C_password
          ="";
                  
          //獲取全部Cookie
                  Cookie c[]=request.getCookies();
                  
          for(i=0;i<c.length;i++)
                  {
                      
          //在Cookie中查找用戶(hù)名、密碼,如果找到,則分別將其賦值給用戶(hù)名、密碼變量
                      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中有用戶(hù)名、密碼,將用戶(hù)名、密碼提交到驗(yàn)證頁(yè)面
                      response.sendRedirect("check.jsp?username="+C_username+"&password="+C_password);
                  }
                  
          else
                  {
                  
          //Cookie中沒(méi)有用戶(hù)名、密碼,跳轉(zhuǎn)到登錄頁(yè)面
              %>
                  
          <jsp:forward page="login.jsp" />
              
          <%
                  }
              
          %>
            
          </body>
          </html>

          login.jsp:

          <%@ page contentType="text/html;charset=GB2312" %>
          <html>
            
          <head>
              
          <title>登錄</title>
            
          </head>
            
            
          <body>
                
          <center>
                
          <h1>登錄頁(yè)面</h1>
                
          <hr>
              
          <form action="check.jsp" method="post">
                  
          <table>
                      
          <tr>
                          
          <td>用戶(hù)名:</td>
                          
          <td><input type="text" name="username" /></td>
                      
          </tr>
                      
          <tr>
                          
          <td>密  碼:</td>
                          
          <td><input type="password" name="password" /></td>
                      
          </tr>
                      
          <tr>
                      
          <td>Cookie選項(xiàng):</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>驗(yàn)證頁(yè)面</title>
            
          </head>
            
            
          <body>
              
          <%
                  String Username
          =request.getParameter("username");
                  String Password
          =request.getParameter("password");
                  String IsCookie
          =request.getParameter("cookie");
                  
          //判斷用戶(hù)名、密碼的合法性
                  if("magci".equals(Username) && "123456".equals(Password))
                  
          //為了避免空指向異常,比較兩個(gè)字符串時(shí),如有字符串常量,則使用字符串常量的“equals”方法(即將常量寫(xiě)在前面)。
                  {
                      
          //合法用戶(hù)
                      if("save".equals(IsCookie))
                      {
                          
          //如果選擇了保存Cookie選項(xiàng),則保存Cookie
                          Cookie c1=new Cookie("username",Username);
                          Cookie c2
          =new Cookie("password",Password);
                          
          //設(shè)置Cookie保存時(shí)間為1分鐘
                          c1.setMaxAge(60);
                          c2.setMaxAge(
          60);
                          response.addCookie(c1);
                          response.addCookie(c2);
                      }
                      
          //跳轉(zhuǎn)到歡迎頁(yè)面
                      %>
                      
          <jsp:forward page="pass.jsp"/>
                      
          <%
                  }
                  
          else
                  {
                      
          //非法用戶(hù),跳轉(zhuǎn)到登錄失敗頁(yè)面
                      %>
                      
          <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跳轉(zhuǎn)為服務(wù)器端跳轉(zhuǎn),跳轉(zhuǎn)后仍在check.jsp頁(yè)面,可以繼續(xù)使用usename參數(shù) --%>
              
          <%=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) 評(píng)論(3)  編輯  收藏

          評(píng)論:
          # re: 使用COOKIE登錄驗(yàn)證(轉(zhuǎn)載) 2012-03-21 09:48 | ghg
          gfhgfhgf  回復(fù)  更多評(píng)論
            
          # re: 使用COOKIE登錄驗(yàn)證asd(轉(zhuǎn)載) 2014-05-13 19:47 | asdas
          # re: 使用COOKIE登錄驗(yàn)證(轉(zhuǎn)載) 2016-01-04 15:11 | vfdgv
          fsfsdvsdfv  回復(fù)  更多評(píng)論
            

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 常宁市| 额尔古纳市| 蕉岭县| 大洼县| 三都| 城口县| 开封市| 吉木萨尔县| 阿拉善右旗| 科尔| 西平县| 遵化市| 富宁县| 融水| 辛集市| 公安县| 湖北省| 广饶县| 舟山市| 舒兰市| 专栏| 云南省| 应城市| 中阳县| 大同市| 金沙县| 宁德市| 阿克苏市| 库尔勒市| 临高县| 辽阳市| 湄潭县| 龙井市| 年辖:市辖区| 松桃| 扎赉特旗| 黎川县| 郸城县| 织金县| 洛隆县| 保山市|