Java–用户登录(JDBC,MYSQL,Servlet)
博客说明
文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!
用户登录案例需求
编写login.html登录页面username & password 两个输入框
使用Druid数据库连接池技术,操作mysql,day14数据库中user表
使用JdbcTemplate技术封装JDBC
登录成功跳转到SuccessServlet展示:登录成功!用户名,欢迎您
登录失败跳转到FailServlet展示:登录失败,用户名或密码错误
登录页面 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <title > Title</title > </head > <body > <form action ="/login_test/loginServlet" method ="post" > 用户名:<input type ="text" name ="username" > <br > 密码:<input type ="password" name ="password" > <br > <input type ="submit" value ="登录" > </form > </body > </html >
创建数据库 1 2 3 4 5 6 7 8 9 CREATE DATABASE login; USE login; CREATE TABLE user( id INT PRIMARY KEY auto_increment, username VARCHAR(32) UNIQUE NOT NULL, password VARCHAR(32) NOT NULL )
配置文件 1 2 3 4 5 6 7 driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql:///login username=root password=root initialSize=5 maxActive=10 maxWait=3000
项目依赖
创建用户实体类 创建包cn.guizimo.domain,创建类User
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 package cn.guizimo.domain;public class User { private int id; private String username; private String password; public int getId () { return id; } public void setId (int id) { this .id = id; } public String getUsername () { return username; } public void setUsername (String username) { this .username = username; } public String getPassword () { return password; } public void setPassword (String password) { this .password = password; } @Override public String toString () { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}' ; } }
创建工具类 创建包cn.guizimo.util,编写工具类JDBCUtils
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 package cn.guizimo.util;import com.alibaba.druid.pool.DruidDataSourceFactory;import javax.sql.DataSource;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.SQLException;import java.util.Properties;public class JDBCUtils { private static DataSource ds ; static { try { Properties pro = new Properties(); InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); pro.load(is); ds = DruidDataSourceFactory.createDataSource(pro); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public static DataSource getDataSource () { return ds; } public static Connection getConnection () throws SQLException { return ds.getConnection(); } }
创建数据操作类 创建包cn.guizimo.dao,创建类UserDao,提供login方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package cn.guizimo.dao;import cn.guizimo.domain.User;import cn.guizimo.util.JDBCUtils;import org.springframework.dao.DataAccessException;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.JdbcTemplate;public class UserDao { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); public User login (User loginUser) { try { String sql = "select * from user where username = ? and password = ?" ; User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class ), loginUser .getUsername (), loginUser .getPassword ()) ; return user; } catch (DataAccessException e) { e.printStackTrace(); return null ; } } }
创建servlet类 cn.guizimo.web.servlet.LoginServlet类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 package cn.guizimo.web.servlet;import cn.guizimo.dao.UserDao;import cn.guizimo.domain.User;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@WebServlet ("/loginServlet" )public class LoginServlet extends HttpServlet { @Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8" ); String username = req.getParameter("username" ); String password = req.getParameter("password" ); User loginUser = new User(); loginUser.setUsername(username); loginUser.setPassword(password); UserDao dao = new UserDao(); User user = dao.login(loginUser); if (user == null ){ req.getRequestDispatcher("/failServlet" ).forward(req,resp); }else { req.setAttribute("user" ,user); req.getRequestDispatcher("/successServlet" ).forward(req,resp); } } @Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this .doGet(req,resp); } }
cn.guizimo.web.servlet.FailServlet类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package cn.guizimo.web.servlet;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@WebServlet ("/failServlet" )public class FailServlet extends HttpServlet { @Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this .doPost(req, resp); } @Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8" ); resp.getWriter().write("登录失败,用户名或密码错误" ); } }
cn.guizimo.web.servlet.SuccessServlet类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package cn.guizimo.web.servlet;import cn.guizimo.domain.User;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@WebServlet ("/successServlet" )public class SuccessServlet extends HttpServlet { @Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this .doPost(req, resp); } @Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User user = (User) req.getAttribute("user" ); if (user != null ){ resp.setContentType("text/html;charset=utf-8" ); resp.getWriter().write("登录成功!" +user.getUsername()+",欢迎您" ); } } }
运行 在浏览器中打开http://localhost:8080/login_test/login.html
登录成功
登录失败
感谢
万能的网络
以及勤劳的自己