1. index.jsp来提交请求
2. login.jsp来检查数据库数据
3. 需要注意的是,下载下来的mysql.conection jar包需要放到WEB-INF下面的lib文件夹下
jsp projectWEB-INFlibmysql-connector-java-5.1.25-bin.jar
1 <%@ page contentType="text/html" pageEncoding="utf-8"%> 2 <html> 3 <head> 4 <title> 5 login page 6 </title> 7 </head> 8 9 <body> 10 <center> 11 <h1> 12 登录页面 13 </h1> 14 </br> 15 </br> 16 <form action="jsp/login.jsp" method="post"> 17 <table id="loginTable"> 18 <tr> 19 <tb>用户名:</tb> 20 <tb><input type="text" name="name"/><br></tb> 21 </tr> 22 <tr> 23 <tb>密码:</tb> 24 <tb><input type="password" name="passwd"/><br></tb> 25 </tr> 26 <tr> 27 <tb><input type="submit"></tb> 28 <tb><input type="reset"></tb> 29 </tr> 30 </table> 31 </form> 32 </center> 33 </body> 34 35 </html>
1 <%@ page contentType="text/html" pageEncoding="utf-8"%> 2 <%@ page language="java"%> 3 <%@ page import="com.mysql.jdbc.Driver" %> 4 <%@ page import="java.sql.*" %> 5 <html> 6 <head> 7 <title> 8 check page 9 </title> 10 </head> 11 12 <body> 13 <% 14 String driverName="com.mysql.jdbc.Driver"; 15 String userName="root"; 16 String userPasswd="huawei"; 17 String url="jdbc:mysql://localhost:3306/connect"; 18 String result="false"; 19 PreparedStatement statement = null; 20 Class.forName("com.mysql.jdbc.Driver").newInstance(); 21 Connection connection=DriverManager.getConnection(url,userName,userPasswd); 22 ResultSet resultSet = null; 23 String loginName=request.getParameter("name"); 24 String password=request.getParameter("passwd"); 25 String tip = ""; 26 if(connection != null) 27 { 28 statement= connection.prepareStatement("select passwd from userinfo where username='"+loginName+"'"); 29 resultSet = statement.executeQuery(); 30 tip = loginName + " login failed."; 31 if(resultSet.next()) 32 { 33 result=resultSet.getString(1); 34 if(result.equals(password)) 35 { 36 tip = loginName + " login successfully."; 37 } 38 } 39 }else 40 { 41 tip = "Data base connection failed."; 42 } 43 %> 44 45 <%=tip%> 46 </body> 47 48 </html>