一、环境
工具:IDEA、mysql、JDK9
二:工程目录
三、代码编写
1.前端页面_login.html
登录
2.数据库表 _ user
CREATE TABLE user(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(10) NOT NULL,
password VARCHAR(10) NOT NULL
);
3.JDBC工具类 _ JDBCUtils
public class JDBCUtils {
private static DataSource ds; //创建成员变量
static {
//创建Properties集合
Properties pp = new Properties();
//使用类加载器加载druid配置文件
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
try {
//使用Properties集合将输入流加载文件
pp.load(is);
//使用工厂类对象加载
ds = DruidDataSourceFactory.createDataSource(pp);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//定义获取数据库连接池对象
public static DataSource getDataSource(){
return ds;
}
}
4.用户实体类_User
//标准的用户类
public class User {
private int id; //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 + '\'' +
'}';
}
}
5.用户登录数据库访问类_UserLoginDao
public class UserLoginDao {
//申明JDBC对象共用
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
//定义方法,用来操作数据库中user表的登录方法
public User login(User userLogin){
try {
//定义sql语句
String sql = "SELECT * FROM user WHERE username =? AND password =?";
//使用template执行sql语句
User user = template.queryForObject(sql, new BeanPropertyRowMapper(User.class),
userLogin.getUsername(), userLogin.getPassword());
return user;
}catch (DataAccessException e){
return null;
}
}
}
6.用户登录Servlet_LoginServlet
@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对象
User loginUser = new User();
loginUser.setUsername(username);
loginUser.setPassword(password);*/
//获取请求对象数据并封装成map集合
Mapmap = req.getParameterMap();
//创建User对象
User loginUser = new User();
try {
//将map封装到loginUser对象中
BeanUtils.populate(loginUser,map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//创建UserLoginDao对象
UserLoginDao uld = new UserLoginDao();
//调用UserLoginDao的login方法
User user = uld.login(loginUser);
//判断
if (user ==null){
//跳转到successServlet
req.getRequestDispatcher("/failedServlet").forward(req,resp);
}else {
//设置共享域
req.setAttribute("user",user);
//跳转到failedServlet
req.getRequestDispatcher("/successServlet").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
7.用户登陆成功Servlet_LoginSuccessServlet
@WebServlet("/successServlet")
public class SuccessServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置字符编码
response.setContentType("text/html;charset=utf-8");
//获取共享域数据
User user = (User) request.getAttribute("user");
//输出
response.getWriter().write("登陆成功,"+user.getUsername()+"欢迎您");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
8.用户登陆失败Servlet_LoginFailedServlet
@WebServlet("/failedServlet")
public class FailedServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("登录失败,用户名或密码错误");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
9.效果展示
*这里我输入用户名:zhangsan 密码:123456
*浏览器页面上输出登陆成功,并显示登录的用户名
*这里我输入用户名:zzz 密码:123456 这个用户名是不存在的
*显示提示信息用户名或者密码错误