The software used in this experiment is myeclipse10, tomcat7, Dreamweaver, sqlserver2008 database. Users can log in using their username and password.
Recommended course: Java Tutorial.
#If the login is successful, the page will display the login success. If the password is incorrect, the page will display the login failure. To connect to the database using the javabean method, you need to download the sqlserver2008 driver, create a new package "Bean" in the src folder under the web project folder, and create a new "DBBean.java" file under this package.
DBBean.java file code is as follows:
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) { } } }
There are three jsp page files in the WEBROOT directory: login.jsp, logincheck.jsp, loginsuccess.jsp. In the login.jsp page, you can enter the user name and password and click the login button to jump to the loginsucccess.jsp page if the login is successful. If the password is incorrect, the page will jump to the failed login page. (Of course, before jumping to the page, you need to create a new database in sqlserver2008, create a new table in the database directory, and fill in the table information)
Screenshot of the folder structure:
login.jsp login interface code:
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 login failure code:
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('账号错误——else');</script>"); response.setHeader("refresh", "0;url=login.jsp"); } %>
indexsuccess.jsp login success code:
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>
The final page effect is as follows:
If everything is correct, the following page will be displayed:
If the password is incorrect, the following page will be displayed:
The above is the detailed content of How to write login interface in jsp. For more information, please follow other related articles on the PHP Chinese website!