<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-51  評(píng)論-14  文章-0  trackbacks-0
    附加功能:登錄成功后保存Cookie一段時(shí)間,在這期間無(wú)需重新登錄
    一個(gè)基本的登錄模塊至少分為4個(gè)頁(yè)面:
    1.輸入用戶信息頁(yè)面(login.jsp);
    2.用戶合法性驗(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ú)用戶名、密碼
              ----------------------
              |   Y                 N  |
              |                          V
              |                      login.jsp<--------------------
              |                          |輸入用戶名、密碼           |
              |                          V                                  |
              ---------------->check.jsp                           |
                                          |                                  |
                                          |驗(yàn)證用戶名、密碼          |
                              ---------------------                     |
                              |  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中有用戶名、密碼,將用戶名、密碼提交到驗(yàn)證頁(yè)面
                response.sendRedirect("check.jsp?username="+C_username+"&password="+C_password);
            }
            
    else
            {
            
    //Cookie中沒(méi)有用戶名、密碼,跳轉(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>用戶名:</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");
            
    //判斷用戶名、密碼的合法性
            if("magci".equals(Username) && "123456".equals(Password))
            
    //為了避免空指向異常,比較兩個(gè)字符串時(shí),如有字符串常量,則使用字符串常量的“equals”方法(即將常量寫(xiě)在前面)。
            {
                
    //合法用戶
                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
            {
                
    //非法用戶,跳轉(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 閱讀(5201) 評(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è)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲色丰满少妇高潮18p| AV无码免费永久在线观看| 亚洲色偷精品一区二区三区| 日本亚洲成高清一区二区三区| 国产成人免费永久播放视频平台| 91高清免费国产自产拍2021| 中文字幕成人免费高清在线| 处破女第一次亚洲18分钟| 亚洲av无码电影网| 亚洲欧洲日产专区| 无码乱人伦一区二区亚洲| 亚洲线精品一区二区三区| 亚洲精品成人久久久| 国产成人精品免费视频大全五级 | 国产婷婷高清在线观看免费| 2021国产精品成人免费视频| 久久国产色AV免费观看| 久久免费视频精品| 东方aⅴ免费观看久久av| eeuss影院www天堂免费| 一级毛片免费播放试看60分钟| 麻豆安全免费网址入口| 日韩大片在线永久免费观看网站| 久久亚洲AV成人无码国产电影| 亚洲精品乱码久久久久蜜桃| 亚洲Av永久无码精品一区二区| 亚洲欧美成人综合久久久| 亚洲人成电影网站色| 亚洲综合无码一区二区痴汉| 亚洲国产日韩视频观看| 亚洲欧洲精品成人久久曰| 亚洲另类无码专区首页| 自拍偷自拍亚洲精品播放| 国产青草亚洲香蕉精品久久| 极品色天使在线婷婷天堂亚洲| 国产精品亚洲精品日韩电影| 日本黄页网址在线看免费不卡| 成人午夜影视全部免费看| 成人毛片100免费观看| 麻豆精品成人免费国产片| 在线人成精品免费视频|