騎豬闖天下

          J2ME隨筆,記錄成長的腳步

          統(tǒng)計

          留言簿(3)

          閱讀排行榜

          評論排行榜

          [HTML-原創(chuàng)] Web頁面實現(xiàn)登錄驗證 范例

          Web頁面實現(xiàn)登錄驗證

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

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

          <html>
            
          <head>
              
          <title>系統(tǒng)登錄</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>
              版權(quán)所有:杜長風(fēng) 
          <br>
              發(fā)布時間:20080919
          <br>
              
              
              
          <center>
              
          <h2>系統(tǒng)登錄</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{
                      
          //連接數(shù)據(jù)庫
                      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"); //驗證通過后,跳轉(zhuǎn)到后續(xù)界面
                          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"%>
                  歡迎進(jìn)入本頁面,您已經(jīng)通過驗證,你的用戶名是:
          <%=session.getAttribute("uname")%><p>
                      
          <href="continue.jsp"> 你可以跳轉(zhuǎn)到后續(xù)界面</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"))
              {
                  
          //檢查未通過,跳轉(zhuǎn)回登錄界面
                  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"%>,歡迎您進(jìn)入第二個頁面!<br>
                  
          <% out.println("<a href=index.html>返回</a>"); %>
              
          </center>
            
          </body>
          </html>

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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 长葛市| 兴海县| 夏津县| 永吉县| 崇阳县| 东丽区| 新化县| 伊宁市| 阿拉善盟| 广河县| 惠安县| 吴忠市| 罗平县| 称多县| 通渭县| 渭源县| 康乐县| 三原县| 凤冈县| 德钦县| 那曲县| 宜宾市| 安顺市| 嵩明县| 澎湖县| 汝城县| 襄垣县| 江门市| 岱山县| 宜川县| 香格里拉县| 花莲县| 鹤山市| 衡水市| 兖州市| 志丹县| 金平| 济南市| 会泽县| 留坝县| 固原市|