登录界面login.jsp
1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>欢迎来到登录界面</title> 9 10 <script type="text/javascript"> <!--验证文本框内是否为空--> 11 function check() { 12 var username = document.getElementById("username"); 13 var userInfo = document.getElementById("usernameInfo"); 14 userInfo.innerHTML = ""; 15 if(username.value == "") { 16 17 userInfo.innerHTML = "用户名不能为空!"; 18 username.focus(); 19 return false; 20 } 21 var password = document.getElementById("password"); 22 var passinfo = document.getElementById("pwd"); 23 passinfo.innerHTML = ""; 24 if(password.value == "") { 25 passinfo.innerHTML = "密码不能为空!"; 26 password.focus(); 27 return false; 28 } 29 return true; 30 } 31 </script> 32 33 </head> 34 35 <body background="1.jpg"> <!--背景图片--> 36 37 <center> 38 <h1>用户登录</h1> 39 </center> 40 <br> 41 <center> 42 <form name="form2" action="logincheck.jsp" method="post" onsubmit="return check()"> 43 <table border="0"> 44 <tr> 45 <td>用户名:</td> 46 <td><input type="text" name="username" id="username"></td> 47 <td><span id="usernameInfo"></span></td> 48 </tr> 49 <tr> 50 <td>密码:</td> 51 <td><input type="password" name="password" id="password"></td> 52 <td><span id="pwd"></span></td> 53 </tr> 54 </table> 55 <br> 56 <input type="submit" value="登录" style="color:#BC8F8F"> 57 58 </form> 59
62 63 </center> 64 65 </body> 66 </html>
验证界面logincheck.jsp
<%@ page language="java" import="java.util.*" import="java.sql.*" 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> <% Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; String driverName = "com.mysql.jdbc.Driver"; String userName = "root"; String userPwd = "登录数据库密码"; String dbName = "数据库的名字"; String url1 = "jdbc:mysql://localhost:3306/" + dbName; String url2 = "?user=" + userName + "&password=" + userPwd; String url3 = "&useUnicode=true&characterEncoding=UTF-8"; String url = url1 + url2 + url3; request.setCharacterEncoding("UTF-8"); Class.forName(driverName); conn = DriverManager.getConnection(url); String sql = "select * from 表格名字 where user=? and pwd=?"; <!--user,pwd是表格的两个项目--> pstmt = conn.prepareStatement(sql); String user = request.getParameter("username"); String password = request.getParameter("password"); pstmt.setString(1, user); pstmt.setString(2, password); rs = pstmt.executeQuery(); if(rs.next()) { %><center><h1>登陆成功!</h1></center> <%} else { %><center><h1>用户名或密码错误!</h1></center> <%} if(rs != null) { rs.close(); } if(pstmt != null) { pstmt.close(); } if(conn != null) { conn.close(); } %> </body> </html>
结构目录
将截图中lib文件夹下的文件直接复制到那里(可以直接在网上下载下来)
数据库MYSQL截图