JSP基礎文法<二>,JSP+JDBC使用者登入程式實現,jspjdbc

來源:互聯網
上載者:User

JSP基礎文法<二>,JSP+JDBC使用者登入程式實現,jspjdbc
1.1 建立資料庫表

表名:user

欄位:

  userid   儲存使用者的登入id

  name 使用者名稱

  password 密碼

 

1.2 實現思路

  a. 使用者登入,則需要有個一個表單頁,此頁面可輸入使用者的登入id和密碼;

  b.提交到一個驗證的jsp頁面進行資料庫的操作驗證,如果可查詢到使用者資訊,則為合法使用者,則跳轉至登入成功頁;

  c.如果未查詢到,則使用者不是合法使用者,則跳轉錯誤頁進行提示;

  

1.3 程式實現

login.jsp  : 提供使用者的登入表單,可輸入登入id及密碼;

<%@page contentType="text/html" pageEncoding="GBK"%><html><head><title>登入頁面</title></head><body><center><h1>登陸操作</h1><hr>    <form action="login_check.jsp" method="post">        <table>            <tr>                <td colspan="2">                    使用者登陸                </td>            </tr>            <tr>                <td>登陸ID:</td>                <td><input type="text" name="id"></td>            </tr>            <tr>                <td>登陸密碼:</td>                <td><input type="password" name="password"></td>            </tr>            <tr>                <td colspan="2">                    <input type="submit" value="登陸">                    <input type="reset" value="重設">                </td>            </tr>        </table>    </form></center></body></html>

login_check.jsp : 登入檢查頁,根部表單提交過來的資訊進行驗證,成功登入成功頁,否則跳轉到錯誤提示頁面;

<%@page contentType="text/html" pageEncoding="GBK"%><%@page import="java.sql.*"%><html><head><title>驗證頁面</title></head><body><center><h1>登陸操作</h1><hr><%!    // 定義若干個資料庫的串連常量    public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;    public static final String DBURL = "jdbc:mysql://localhost:3306/procs" ;    public static final String DBUSER = "root" ;    public static final String DBPASS = "123456" ;%><%    Connection conn = null ;        // 資料庫連接    PreparedStatement pstmt = null ;    // 資料庫預先處理操作    ResultSet rs = null ;        // 查詢要處理結果集    boolean flag = false ;    // 儲存標記    String name = null ;    // 儲存真實姓名%><%try{%><%    Class.forName(DBDRIVER) ;    conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;    String sql = "SELECT name FROM user WHERE id=? AND password=?" ;    pstmt = conn.prepareStatement(sql) ;    pstmt.setString(1,request.getParameter("id")) ;    pstmt.setString(2,request.getParameter("password")) ;    rs = pstmt.executeQuery() ;    // 查詢    if(rs.next()){    // 如果有資料,則可以執行        flag = true ;    //  表示登陸成功        name = rs.getString(1) ;    }    %><%}catch(Exception e)    {    e.printStackTrace() ;}finally{    try{        rs.close() ;        pstmt.close() ;        conn.close() ;    } catch(Exception e){}}%><%    if(flag){    // 登陸成功%>        <jsp:forward page="login_success.jsp">            <jsp:param name="uname" value="<%=name%>"/>        </jsp:forward><%    } else {        // 登陸失敗%>        <jsp:forward page="login_failure.jsp"/><%    }%></center></body></html>

login_success.jsp 登入成功頁,顯示歡迎資訊;

<%@page contentType="text/html" pageEncoding="GBK"%><html><head><title>驗證成功</title></head><body><center><h1>登陸操作</h1><h2>登陸成功</h2><h2>歡迎<font color="red"><%=request.getParameter("uname")%></font>光臨!</h2></center></body></html>

login_failure.jsp : 登入失敗頁,提示使用者輸入錯誤,並提供重新登入的超連結;

<%@page contentType="text/html" pageEncoding="GBK"%><html><head><title>登入失敗頁面</title></head><body><center><h1>登陸操作</h1><h2>登陸失敗,請重新<a href="login.jsp">登陸</a>!</h2></center></body></html>

   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.