需求:
登录功能
登录页面输入用户名和密码, 到数据库进行验证 ,如果成功跳转到success.html页面,失败跳转到error.html页面
数据库 mysql,数据表 t_user表【表中的字段 :name 用户名,pwd 密码】
实现:
mysql 存储数据建表
jdbc操作java代码连接查询数据库里对应的字段
servlet将 html和java连接起来,用login来登录转发到成功或者失败页面。
实现步骤:
jdbc
依赖 jar包,数据库连接的util DBUtil数据库连接工具类
mysql 连接
db.properties 数据库连接配置
核心代码实现:(DAO层)
sql语句 查询:String sql = "select * from t_user where name=? and pwd =? ";
创建一个实体类和数据库表对应,dao层 用户名和密码验证,返回一个结果 对象
servlet调用DAO层:如果用户存在 ,不为空,跳转到ok.html页面 ;如果不存在,对象为空, 登录失败 error.html
前台页面:
login.html【登录界面】
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>login.html</title> 5 <meta charset="utf-8"> 6 <meta name="keywords" content="keyword1,keyword2,keyword3"> 7 <meta name="description" content="this is my page"> 8 <meta name="content-type" content="text/html; charset=UTF-8"> 9 10 <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> 11 12 </head> 13 14 <body> 15 <form action="UserLogin"> 16 用户名:<input type="text" name="uname" id="uname" /><br> 17 密码:<input type="password" name="pwd" id="pwd" /><br> 18 <input value="登录" type="submit"/> 19 </form> 20 </body> 21 </html>
success.html【登录成功界面】
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>success.html</title> 5 <meta charset="utf-8"> 6 <meta name="keywords" content="keyword1,keyword2,keyword3"> 7 <meta name="description" content="this is my page"> 8 <meta name="content-type" content="text/html; charset=UTF-8"> 9 10 <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> 11 12 </head> 13 14 <body> 15 This is 登录成功 page.看到女神<br> 16 <div> 17 <img src="imges/success.jpg" width="800px" height="500px"/> 18 </div> 19 </body> 20 </html>
error.html【登录失败界面】
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>error.html</title> 5 <meta charset="utf-8"> 6 <meta name="keywords" content="keyword1,keyword2,keyword3"> 7 <meta name="description" content="this is my page"> 8 <meta name="content-type" content="text/html; charset=UTF-8"> 9 10 <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> 11 12 </head> 13 14 <body> 15 This is 登录失败 page.一直在转悠转悠 <br> 16 <div> 17 <img src="imges/error.gif" /> 18 </div> 19 </body> 20 </html>
entity层【实体类】
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package boom.servlet.entity; 2 /** 3 * 实体类 4 * @author Administrator 5 * 6 */ 7 public class T_user { 8 // 对应着数据库表的字段 9 private int id; 10 private String name; 11 private String pwd; 12 // 无参构造方法 13 public T_user() { 14 } 15 // 有参构造方法 16 public T_user(int id, String name, String pwd) { 17 super(); 18 this.id = id; 19 this.name = name; 20 this.pwd = pwd; 21 } 22 // getter setter 23 public int getId() { 24 return id; 25 } 26 public void setId(int id) { 27 this.id = id; 28 } 29 public String getName() { 30 return name; 31 } 32 public void setName(String name) { 33 this.name = name; 34 } 35 public String getPwd() { 36 return pwd; 37 } 38 public void setPwd(String pwd) { 39 this.pwd = pwd; 40 } 41 // 重写toString 42 @Override 43 public String toString() { 44 return "T_user [id=" + id + ", name=" + name + ", pwd=" + pwd + "]"; 45 } 46 47 }
util层【封装的JDBC数据库连接】
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package boom.servlet.util; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.sql.Connection; 6 import java.sql.DriverManager; 7 import java.sql.PreparedStatement; 8 import java.sql.ResultSet; 9 import java.sql.SQLException; 10 import java.sql.Statement; 11 import java.util.Properties; 12 /** 13 * 连接数据库的工具类 14 * @author Administrator 15 * 16 */ 17 public class DBUtil { 18 private static String driver; 19 private static String url; 20 private static String username; 21 private static String password; 22 23 static { 24 // 创建properties对象获取属性文件的内容 25 Properties p = new Properties(); 26 // 获取属性配置文件的读取流对象 27 InputStream is = DBUtil.class.getResourceAsStream("/db.properties"); 28 try { 29 // 加载属性配置文件 30 p.load(is); 31 // 获取jdbc参数 32 driver = p.getProperty("driver"); 33 url = p.getProperty("url"); 34 username = p.getProperty("username"); 35 password = p.getProperty("password"); 36 // 加载驱动 37 Class.forName(driver); 38 } catch (IOException e) { 39 e.printStackTrace(); 40 } catch (ClassNotFoundException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } 44 } 45 46 // 获取Connection对象 47 public static Connection getConnection() { 48 Connection conn = null; 49 try { 50 conn = DriverManager.getConnection(url, username, password); 51 } catch (SQLException e) { 52 e.printStackTrace(); 53 } 54 return conn; 55 } 56 57 // 封装获取PreparedStatement对象 58 public static PreparedStatement getPreparedStatement(String sql, 59 Connection conn) { 60 61 PreparedStatement ps = null; 62 try { 63 ps = conn.prepareStatement(sql); 64 } catch (SQLException e) { 65 // TODO Auto-generated catch block 66 e.printStackTrace(); 67 } 68 return ps; 69 70 } 71 72 // 封装获取Statement对象 73 public static Statement getStatement(Connection conn) { 74 Statement stmt = null; 75 try { 76 stmt = conn.createStatement(); 77 } catch (SQLException e) { 78 e.printStackTrace(); 79 } 80 return stmt; 81 82 } 83 84 // 关闭资源 85 //父类可以调用 子类继承过的父类方法 86 public static void closeAll(ResultSet rs, Statement ps, Connection conn) { 87 try { 88 if(rs!=null ){ 89 rs.close(); 90 } 91 } catch (SQLException e1) { 92 e1.printStackTrace(); 93 } 94 try { 95 if(ps!=null ){ 96 ps.close(); 97 } 98 } catch (SQLException e) { 99 e.printStackTrace(); 100 } 101 try { 102 if(conn!=null){ 103 conn.close(); 104 } 105 } catch (SQLException e) { 106 e.printStackTrace(); 107 } 108 } 109 // 封装DML 110 public static int executeDML(String sql, Object... objs) { 111 // 创建连接对象 112 Connection conn = getConnection(); 113 // 创建sql命令对象 114 PreparedStatement ps = DBUtil.getPreparedStatement(sql, conn); 115 // 给占位符赋值 116 try { 117 conn.setAutoCommit(false); 118 for (int i = 0; i < objs.length; i++) { 119 ps.setObject(i + 1, objs[i]); 120 } 121 int i = ps.executeUpdate(); 122 conn.commit(); 123 return i; 124 } catch (Exception e) { 125 try { 126 conn.rollback(); 127 } catch (SQLException e1) { 128 // TODO Auto-generated catch block 129 e1.printStackTrace(); 130 } 131 } finally { 132 // 关闭资源 133 DBUtil.closeAll(null, ps, conn); 134 } 135 // 返回结果 136 return -1; 137 } 138 // 测试数据库是否连接成功 139 public static void main(String[] args) { 140 Connection conn = DBUtil.getConnection(); 141 System.out.println(conn); 142 } 143 }
dao层【user实现类】
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package boom.servlet.dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 import boom.servlet.entity.T_user; 9 import boom.servlet.util.DBUtil; 10 11 /** 12 * 登录查询实现类 13 * @author Administrator 14 *select * from t_user where name='admin' and pwd='admin' 15 */ 16 public class UserDaoImpl { 17 // 返回对象 18 public T_user getUser(String name,String pwd){ 19 String sql = "select * from t_user where name=? and pwd =? "; 20 Connection conn = null; 21 PreparedStatement ps = null; 22 ResultSet rs = null; 23 T_user user= null; 24 //1.创建连接 25 conn = DBUtil.getConnection(); 26 try { 27 //2.获取预处理块对象 preparestatement 28 ps = conn.prepareStatement(sql); 29 //3.绑定参数 30 ps.setString(1, name); 31 ps.setString(2, pwd); 32 //4.执行SQL语句 5.获取结果集 33 rs = ps.executeQuery(); 34 //6.遍历结果集存放到 user对象中 35 while(rs.next()){ 36 int id = rs.getInt("id"); 37 String name2 = rs.getString("name"); 38 String pwd2 = rs.getString("pwd"); 39 //将数据放到对象中 40 user = new T_user(id,name2,pwd2); 41 } 42 } catch (SQLException e) { 43 e.printStackTrace(); 44 }finally{ 45 //7.关闭资源 46 DBUtil.closeAll(rs, ps, conn); 47 } 48 return user; 49 } 50 }
servlet层【获取前端页面登录验证】
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package boom.servlet.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import boom.servlet.dao.UserDaoImpl; 11 import boom.servlet.entity.T_user; 12 /** 13 * 获取前端页面登录验证 14 * @author Administrator 15 * 16 */ 17 public class UserLogin extends HttpServlet { 18 @Override 19 protected void service(HttpServletRequest request, 20 HttpServletResponse response) throws ServletException, IOException { 21 // 1、获取login用户提交的数据【账户,密码】 22 String uname = request.getParameter("uname"); 23 String pwd = request.getParameter("pwd"); 24 // 2、调用业务逻辑层。简化后调用dao层 25 UserDaoImpl daoImpl = new UserDaoImpl(); 26 T_user user = daoImpl.getUser(uname, pwd); 27 String path = "/login.html"; 28 if (user != null) { 29 path = "/success.html"; 30 } else { 31 path = "/error.html"; 32 } 33 // 3 根据dao的查询结果 ,跳转到成功或失败页面 34 request.getRequestDispatcher(path).forward(request, response); 35 36 } 37 }
test层【测试dao层是否连接成功】
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package boom.servlet.test; 2 3 import boom.servlet.dao.UserDaoImpl; 4 import boom.servlet.entity.T_user; 5 6 /** 7 * 测试dao层 8 * @author Administrator 9 * 10 */ 11 public class TestUserDao { 12 public static void main(String[] args) { 13 UserDaoImpl daoImpl = new UserDaoImpl(); 14 T_user user = daoImpl.getUser("boom", "123"); 15 if(user != null){ 16 System.out.println("登录成功"); 17 }else{ 18 System.out.println("登录失败"); 19 } 20 } 21 }
数据库配置文件【db.properties】
演示界面: