首頁  >  文章  >  Java  >  jsp裡怎麼寫登入介面

jsp裡怎麼寫登入介面

(*-*)浩
(*-*)浩原創
2019-05-15 11:51:1215297瀏覽

這次試驗所用到的軟體是myeclipse10,tomcat7,Dreamweaver,sqlserver2008資料庫。可以實現使用者使用使用者名稱和密碼登入。

推薦課程:Java教學

jsp裡怎麼寫登入介面

如果登入成功,頁面會顯示登入成功,如果密碼錯誤,則頁面會顯示登入失敗。連接資料庫使用的事javabean方法,需要實現下載好sqlserver2008的驅動程序,在web project資料夾下的src資料夾下新建包“Bean”,並在此包下新建“DBBean.java”文件。

DBBean.java檔案程式碼如下:

package Bean;
import java.sql.*;
public class DBBean {
    private String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=JXP";
    private String dbusername = "sa";
    private String dbpassword = "123456";
    private Connection conn = null;
    private Statement stmt = null;

    public DBBean()
    {
        try
        {
            Class.forName(driverStr);
            conn = DriverManager.getConnection(connStr, dbusername, dbpassword);
            stmt = conn.createStatement();
        } 
        catch (Exception ex) {
            System.out.println("数据连接失败!");
        } 
        
    }

    public int executeUpdate(String s) {
        int result = 0;
        System.out.println("--更新语句:"+s+"\n");
        try {
            result = stmt.executeUpdate(s);
        } catch (Exception ex) {
            System.out.println("执行更新错误!");
        }
        return result;
    }

    public ResultSet executeQuery(String s) {
        ResultSet rs = null;
        System.out.print("--查询语句:"+s+"\n");
        try {
            rs = stmt.executeQuery(s);
        } catch (Exception ex) {
            System.out.println("ִ执行查询错误!");
        }
        return rs;
    }
    public void execQuery(String s){
        try {
            stmt.executeUpdate(s);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("执行插入错误!");
        }
    }

    public void close() {
        try {
            stmt.close();
            conn.close();
        } catch (Exception e) {
        }
    }
}

在WEBROOT目錄下有三個jsp頁面檔案:分別是login.jsp,logincheck.jsp,loginsuccess.jsp.在login.jsp頁面中,可以透過輸入使用者名稱、密碼,點選登入按鈕,實現登入成功loginsucccess.jsp頁面的跳轉,如果密碼錯誤,則頁面會跳到登入失敗的頁面。 (當然,在進行頁面跳轉之前,需要在sqlserver2008中新建一個資料庫,在資料庫目錄下新建一個表,並填入表格的資訊)

資料夾結構截圖:

jsp裡怎麼寫登入介面

login.jsp登入介面程式碼:

nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<meta>
<title>登录界面</title>


    <center>
        <h1>登录</h1>
            <form>
                <table>
                    <tr>
                        <td>账号:</td>
                        <td><input></td>
                    </tr>
                    <tr>
                        <td>密码:</td>
                        <td>
<input>
                        </td>
                    </tr>
                </table>
            <br>
                <input>
            </form>
            <form>
                <input>
            </form>
    </center>

indexcheck.jsp登入失敗代碼:

#
nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<meta>
<title>Insert title here</title>


<usebean></usebean>
 alert('密码错误');");
            response.setHeader("refresh", "0;url=login.jsp");
        }
    }
    else 
    {
        out.print("<script> alert(&#39;账号错误——else&#39;);</script>");
        response.setHeader("refresh", "0;url=login.jsp");
    }
    
%>

indexsuccess.jsp登入成功代碼:

nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<meta>
<title>Insert title here</title>


<h1>登陆成功</h1>

最終的頁面效果如下:

jsp裡怎麼寫登入介面

#如果全都正確,則顯示如下頁面:

jsp裡怎麼寫登入介面

如果密碼錯誤,則顯示如下頁面:

jsp裡怎麼寫登入介面

以上是jsp裡怎麼寫登入介面的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn