小編給大家分享一下jsp里如何寫登錄界面,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
此次試驗所用到的軟件是myeclipse10,tomcat7,Dreamweaver,sqlserver2008數據庫??梢詫崿F用戶使用用戶名和密碼登錄。
如果登錄成功,頁面會顯示登錄成功,如果密碼錯誤,則頁面會顯示登錄失敗。連接數據庫使用的事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> <h2 style="color:red">登錄</h2> <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> <h2>登陸成功</h2> </body> </html>
最終的頁面效果如下:
如果全都正確,則顯示如下頁面:
如果密碼錯誤,則顯示如下頁面:
以上是jsp里如何寫登錄界面的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。