JAVA web簡單的登入介面jsp實現

來源:互聯網
上載者:User

標籤:ring   art   uvc   encoding   md5   sae   jdbc   request   sar   

    此次實驗所用到的軟體是myeclipse10,tomcat7,Dreamweaver,sqlserver2008資料庫。可以實現使用者使用使用者名稱和密碼登入。如果登入成功,頁面會顯示登入成功,如果密碼錯誤,則頁面會顯示登入失敗。串連資料庫使用的事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中建立一個資料庫,在資料庫目錄下建立一個表,並填入表的資訊)

檔案夾結構:

login.jsp代碼:

<%@ page import="java.sql.*" language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>登入介面</title></head><body>    <center>        <h1 style="color:red">登入</h1>            <form id="indexform" name="indexForm" action="logincheck.jsp" method="post">                <table border="0">                    <tr>                        <td>帳號:</td>                        <td><input type="text" name="username"></td>                    </tr>                    <tr>                        <td>密碼:</td>                        <td><input type="password" name="password">                        </td>                    </tr>                </table>            <br>                <input type="submit" value="登入" style="color:#BC8F8F">            </form>            <form action="zhuce.jsp">                <input type="submit" value="註冊" style="color:#BC8F8F">            </form>    </center></body></html>

 

indexcheck.jsp代碼:

<%@ page import="java.sql.*" language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body><jsp:useBean id="db" class="Bean.DBBean" scope="page" /><%    request.setCharacterEncoding("UTF-8");    String username=(String)request.getParameter("username");    String password=(String)request.getParameter("password");//取出login.jsp的值        //下面是資料庫操作    String sql="select * from login where username="+"‘"+username+"‘";//定義一個查詢語句    ResultSet rs=db.executeQuery(sql);//運行上面的語句    if(rs.next())    {        /* if(password.equals(rs.getString(2)))        {                    } */        if(password.equals(rs.getObject("password"))){            response.sendRedirect("loginsuccess.jsp");        }        else{            out.print("<script language=‘javaScript‘> alert(‘密碼錯誤‘);</script>");            response.setHeader("refresh", "0;url=login.jsp");        }    }    else     {        out.print("<script language=‘javaScript‘> alert(‘帳號錯誤——else‘);</script>");        response.setHeader("refresh", "0;url=login.jsp");    }    %></body></html>

indexsuccess.jsp代碼:

<%@ page import="java.sql.*" language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body><h1>登陸成功</h1></body></html>

最終的頁面效果如下:

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

 

JAVA web簡單的登入介面jsp實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.