騎豬闖天下

          J2ME隨筆,記錄成長的腳步

          統計

          留言簿(3)

          閱讀排行榜

          評論排行榜

          [HTML-原創] Web頁面實現登錄驗證 范例

          Web頁面實現登錄驗證

          HTML入門的范例,一句一句敲入的代碼,特此保留,以備查閱。
          騎豬闖天下。

          1. index.html
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

          <html>
            
          <head>
              
          <title>系統登錄</title>
              
              
          <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
              
          <meta http-equiv="description" content="this is my page">
              
          <meta http-equiv="content-type" content="text/html; charset=UTF-8">
              
              
          <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

            
          </head>
            
            
          <body>
              版權所有:杜長風 
          <br>
              發布時間:20080919
          <br>
              
              
              
          <center>
              
          <h2>系統登錄</h2>
              
          <form action="login.jsp" method="post">
                  
          <input type="text" name="uid" maxlength=18 style="width:150"> <br>
                  
          <input tpye="password" name="upwd" maxlength=18 style="width:150"> <br>
                  
          <input type="submit" value="登錄">
                  
          <input type="reset" value="取消">
              
          </form>
              
          </center>
            
          </body>
            
          </html>

          2. login.jsp
          <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%>
          <%@ page import="java.sql.*"%>    


          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            
          <head>
              
              
          <title>驗證頁面</title>
              
              
          <meta http-equiv="pragma" content="no-cache">
              
          <meta http-equiv="cache-control" content="no-cache">
              
          <meta http-equiv="expires" content="0">    
              
          <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
              
          <meta http-equiv="description" content="This is my page">
              
          <!--
              <link rel="stylesheet" type="text/css" href="styles.css">
              
          -->

            
          </head>
            
            
          <body> 
              
          <%
              
          String username = request.getParameter("uid");
              
          String password = request.getParameter("upwd");
              
          if (username != null && !username.equals(""))
              {
                  try{
                      
          //連接數據庫
                      Class.forName(
          "com.mysql.jdbc.Driver"); //注意這里的連接可能有問題
                      Connection conn 
          = DriverManager.getConnection("jdbc:mysql://localhost/test""root""admin");
                      Statement stmt 
          = conn.createStatement();
                      
                      
          String sql = "select * from user where username='" + username + "'";
                      sql 
          += "and password='" + password + "'";
                      
                      ResultSet result 
          = stmt.executeQuery(sql);
                      
          if(result.next())
                      {
                          session.setAttribute(
          "login""ok"); //驗證通過后,跳轉到后續界面
                          session.setAttribute(
          "uname", username);
              
          %>
                              
          <jsp:forward page="main.jsp"/>
                              
              
          <%
                      }
          else 
                          out.println(
          "錯誤的用戶名和密碼");  //驗證未通過,顯示錯誤信息
                          
                      out.println(
          "<a href=index.html>返回</a>");
                  }catch(Exception ee){
                      ee.printStackTrace();
                  }
              }
          else {
                  
                  out.println(
          "請先登錄!");
                  out.println(
          "<a href=index.html>返回</a>");
              }
            
          %>
            
          </body>
          </html>

          3. main.jsp
          <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            
          <head>
              
              
          <title>主頁面</title>
              
              
          <meta http-equiv="pragma" content="no-cache">
              
          <meta http-equiv="cache-control" content="no-cache">
              
          <meta http-equiv="expires" content="0">    
              
          <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
              
          <meta http-equiv="description" content="This is my page">
              
          <!--
              <link rel="stylesheet" type="text/css" href="styles.css">
              
          -->

            
          </head>
            
            
          <body>
              
          <!-- 包含近來驗證身份頁面的源代碼 -->
              
          <%@include file="checkvalid.jsp"%>
                  歡迎進入本頁面,您已經通過驗證,你的用戶名是:
          <%=session.getAttribute("uname")%><p>
                      
          <href="continue.jsp"> 你可以跳轉到后續界面</a>
            
          </body>
          </html>

           

          4. checkvalid.jsp

          <%@ page language="java" import="java.util.*"  contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            
          <head>
              
              
          <title>驗證頁面</title>
              
              
          <meta http-equiv="pragma" content="no-cache">
              
          <meta http-equiv="cache-control" content="no-cache">
              
          <meta http-equiv="expires" content="0">    
              
          <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
              
          <meta http-equiv="description" content="This is my page">
              
          <!--
              <link rel="stylesheet" type="text/css" href="styles.css">
              
          -->

            
          </head>
            
            
          <body>
              This is my JSP page. 
          <br>
              
          <%
              
          if(session.getAttribute("login"== null || !session.getAttribute("login").equals("ok"))
              {
                  
          //檢查未通過,跳轉回登錄界面
                  response.sendRedirect(
          "index.html");
              }
               
          %>
            
          </body>
          </html>

          5. continue.jsp
          <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
            
          <head>
              
              
          <title>Insert title here</title>
              
              
          <meta http-equiv="pragma" content="no-cache">
              
          <meta http-equiv="cache-control" content="no-cache">
              
          <meta http-equiv="expires" content="0">    
              
          <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
              
          <meta http-equiv="description" content="This is my page">
              
          <!--
              <link rel="stylesheet" type="text/css" href="styles.css">
              
          -->

            
          </head>
            
            
          <body>
                
          <center>
              
          <%@include file="checkvalid.jsp" %>
                  
          <%=session.getAttribute("uname"%>,歡迎您進入第二個頁面!<br>
                  
          <% out.println("<a href=index.html>返回</a>"); %>
              
          </center>
            
          </body>
          </html>

          posted on 2008-09-20 14:23 騎豬闖天下 閱讀(2289) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 安庆市| 剑河县| 太湖县| 襄樊市| 宁武县| 普兰店市| 雅安市| 望都县| 古蔺县| 阿合奇县| 苏尼特左旗| 虹口区| 岳西县| 牡丹江市| 海丰县| 永顺县| 益阳市| 铜鼓县| 江西省| 财经| 芮城县| 都江堰市| 涿鹿县| 汨罗市| 九江市| 精河县| 石城县| 武功县| 华安县| 宜州市| 灵寿县| 镇平县| 和政县| 泽州县| 进贤县| 清丰县| 义马市| 资中县| 常宁市| 阜康市| 丽江市|