jsp 實慄 jsp + jdbc 實現登陸html
一個表單頁,輸入用戶登陸和密碼,而後信息提交到jsp頁面進行驗證,若是能夠服務器跳轉到登陸成功頁,失敗,跳轉到錯誤頁java
跳轉的時候窗口的URL地址會發生變化
編寫登陸代碼mysql
<!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset="UTF-8"> <title>登陸</title> </head> <body> <h1>登陸操做</h1> <form action="login_check.jsp" method="post"> <h1>用戶登陸</h1> <p> 登陸id <input type="text" name="id"/> </p> <p> 登陸密碼 <input type="password" name="password"/> </p> <input type="submit" value="登陸"/> <input type="reset" value="重置"/> </form> </body> </html>
<%@ page import="java.sql.*" %> <%-- Created by IntelliJ IDEA. User: ming Date: 19-3-9 Time: 下午5:50 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%! // 數據庫驅動程序 public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver"; // 數據庫鏈接地址 public static final String DBURL = "jdbc:mysql://47.94.95.84:32786/test"; // 用戶名 public static final String DBUSER = "root"; // 密碼 public static final String DBPASS = "ABCcba20170607"; %> <% // 鏈接對象 Connection connection = null; // 操做 PreparedStatement preparedStatement = null; // 結果 ResultSet resultSet = null; // 標誌位 boolean falge = false; // 用戶真實姓名 String name = null; %> <% try{ Class.forName(DBDRIVER); // 得到鏈接 connection = DriverManager.getConnection(DBURL, DBUSER, DBPASS); // 編寫sql驗證ID 密碼 String sql = "SELECT name FROM user WHERE userid = ? AND password = ?"; // 實例化操做對象 preparedStatement = connection.prepareStatement(sql); // 設置查詢內容 preparedStatement.setString(1, request.getParameter("id")); preparedStatement.setString(2, request.getParameter("password")); // 執行查詢 resultSet = preparedStatement.executeQuery(); // 若是能夠查詢到,表示合法用戶 if(resultSet.next()){ name = resultSet.getString(1); // 修改標誌位 falge = true; } }catch (Exception e){ e.printStackTrace(); }finally { try{ resultSet.close(); preparedStatement.close(); connection.close(); }catch (Exception e){ e.printStackTrace(); } } %> <% // 登陸成功 if(falge){ // 進行服務器端跳轉 %> <jsp:forward page="./login_sucess.jsp"> <jsp:param name="uname" value="<%=name%>"/> </jsp:forward> <% }else{ %> <jsp:forward page="./login_failure.html"/> <% } %> </body> </html>
<%-- Created by IntelliJ IDEA. User: ming Date: 19-3-9 Time: 下午10:22 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>登陸成功</h1> <%=request.getParameter("uname")%> </body> </html>
<%-- Created by IntelliJ IDEA. User: ming Date: 19-3-9 Time: 下午10:22 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>登陸成功</h1> <%=request.getParameter("uname")%> </body> </html>