案例:用户登录
- 用户登陆案例需求:
- 编写Login.html登录页面
- 使用Druid数据库连接池技术,操作mysql,day14数据库中的user表
- 使用JdbcTemplate技术封装到JDBC
- 登录成功跳转到SuccessServlet展示:登陆成功!用户名,欢迎你
- 登陆失败跳转到FailServlet展示:登录失败,用户名或密码错误
分析:
开发步骤:
- 创建项目,导入html页面,配置文件,jar包
- 创建数据库环境
create database day14;
USE day14;
create table user(
id int primary key auto_increment,
username varchar(32) UNIQUE not null,
password varchar(32) not null
);
package com.ailyt.domain;
/**
* 用户实体类
*/
public class User {
private int id;
private String username;
private String password;
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public User() {
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
}
package com.ailyt.dao;
import com.ailyt.domain.User;
import com.ailyt.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* 创建数据库中User表的类
*/
public class UserDao {
//声明JDBCTemplate对象共用
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDs());
/**
* 登录方法
* @param loginUser 只用用户名和密码
* @return user包含用户全部数据
*/
public User login(User loginUser) {
//编写sql
String sql = "select * from user where username = ? and password = ?";
//调用query方法
User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), loginUser.getUsername(), loginUser.getPassword());
return user;
}
}
package com.ailyt.servlet;
import com.ailyt.dao.UserDao;
import com.ailyt.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(value = "/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.设置编码
req.setCharacterEncoding("utf8");
//2.获取请求参数
String username = req.getParameter("username");
String password = req.getParameter("password");
//3.封装User对象
User loginUser = new User();
loginUser.setUsername(username);
loginUser.setPassword(password);
//4.调用UserDao的login方法
UserDao userDao = new UserDao();
User user = userDao.login(loginUser);
//5.判断User
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);
}
}
- login.html中form表单的action路径的写法