1.编写login.html登录页面:username & password 两个输入框
2.使用Druid数据库连接池技术,操作mysql,day14数据库中user表
3.使用JdbcTemplate技术封装JDBC
4.登录成功跳转到SuccessServlet展示:登录成功!用户名,欢迎您
5.登录失败跳转到FailServlet展示:登录失败,用户名或密码错误
根据表的字段,定义好相关类型的变量
再实现他们的getter和setter方法
public class User {
private Integer id;
private String username;
private String passwd;
//生成gitter和setter方法
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
}
使用Druid连接池来实现,具体可以看这里
注意:druid.properties要放到resource里面,不然会加载不到
public class JDBCUtils {
// 预定义连接池对象
private static DataSource ds;
static {
// 加载配置文件
Properties pro = new Properties();
try {
pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
// 初始化连接池对象
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();
}
}
这个类是用来操作数据库user表的类
利用Spring的Template来操作数据库,Template的用法参考这里
由于需求中我们要给servlet返回一个User对象,所以我们使用queryForObject()
使用try catch包裹(快捷键:ctrl+alt+t
),如果查询成功就返回封装成类的查询结果user,如果查询失败就返回null
/**
* 操控数据库中的user表的对象
*/
public class UserDao {
//创建JdbcTemplate对象
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
/**
* 登陆方法
* @param loginUser
* @return 查询成功就返回user,查询失败就返回null
*/
public User login(User loginUser){
// 用try catch来包裹,当查询成功就返回user,查询失败就返回null
try {
String sql = "select * from user where username = ? and passwd = ?";
//使用queryForObject方法查询并封装成user类,注意使用RowMapper
User user = template.queryForObject(
sql,
new BeanPropertyRowMapper<User>(User.class),
loginUser.getUsername(),
loginUser.getPasswd()
);
return user;
} catch (DataAccessException e) {
e.printStackTrace();
return null;
}
}
}
写一个测试类来测试一下是否能成功返回
public class DaoTest {
@Test
public void test(){
User user = new User();
user.setUsername("admin");
user.setPasswd("huamang");
UserDao dao = new UserDao();
User returnUser = dao.login(user);
System.out.println(returnUser);
}
}
html中form表单action路径的写法:虚拟目录+Servlet的资源路径
下面就是安装图片里写的步骤来写了:
LoginServlet
@WebServlet("/loginServlet")
public class LoginServlet 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 {
// 设置编码
req.setCharacterEncoding("utf-8");
// 获取参数
String username = req.getParameter("username");
String passwd = req.getParameter("passwd");
// 把参数封装成User对象
User user = new User();
user.setUsername(username);
user.setPasswd(passwd);
// 调用login方法
UserDao dao = new UserDao();
User returnUser = dao.login(user);
// 判断返回值,转发到结果servlet,如果查询成功还要存储数据到Request域
if (returnUser == null){
req.getRequestDispatcher("/failServlet").forward(req,resp);
}else {
// 存储数据到Request域
req.setAttribute("userinfo",user);
req.getRequestDispatcher("/successServlet").forward(req,resp);
}
}
}
FailServlet
@WebServlet("/failServlet")
public class FailServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设定编码,用response
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("登陆失败,用户名或密码错误");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
SuccessServlet
@WebServlet("/successServlet")
public class SuccessServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设定编码,用response
resp.setContentType("text/html;charset=utf-8");
//获取Request域中共享的user对象,由于我们知道这个user是一个User类,所以可以做强制类型转换
// Object user = req.getAttribute("userinfo");
User user = (User) req.getAttribute("userinfo");
resp.getWriter().write("登陆成功,"+user.getUsername()+",欢迎你");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
现在我们涉及的数据比较少,只有username和passwd,要是以后有很多需要封装成User类的话,一个个set一个个get就很麻烦,所以可以用到BeanUtils工具类
req.getParameterMap()
BeanUtils.populate(user,map)
第一个参数是创建的类,第二个参数是map集合loginServlet
@WebServlet("/loginServlet")
public class LoginServlet 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 {
// 设置编码
req.setCharacterEncoding("utf-8");
// // 获取参数
// String username = req.getParameter("username");
// String passwd = req.getParameter("passwd");
// // 把参数封装成User对象
// User user = new User();
// user.setUsername(username);
// user.setPasswd(passwd);
// 获取所有参数到map
Map<String, String[]> map = req.getParameterMap();
// 创建User对象
User user = new User();
// 使用BeanUtils
try {
BeanUtils.populate(user,map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// 调用login方法
UserDao dao = new UserDao();
User returnUser = dao.login(user);
// 判断返回值,转发到结果servlet,如果查询成功还要存储数据到Request域
if (returnUser == null){
req.getRequestDispatcher("/failServlet").forward(req,resp);
}else {
// 存储数据到Request域
req.setAttribute("userinfo",user);
req.getRequestDispatcher("/successServlet").forward(req,resp);
}
}
}
BeanUtils工具类,简化数据封装,用于封装JavaBean
JavaBean:标准的Java类,就是我们在这个案例里面的User类,用来封装一个个的数据
要求:
功能:封装数据
概念:区别成员变量和属性
BeanUtils方法:
setProperty()
getProperty()
populate(Object obj , Map map)
:将map集合的键值对信息,封装到对应的JavaBean对象中