需求
- 案例中出现的jar包在最后附下载链接
- 编写login.html为登陆页面
- 使用Druid数据库连接池,操作mysql数据库中的user表
- 使用jdbcTemplate技术封装JDBC
- 登陆成功跳转到SuccessServlet展示:登陆成功
- 登陆失败跳转到FailServlet展示:登陆失败,用户名或密码错误
开发步骤
表结构:
- 创建项目,写好html文件,配置文件,jar包
- 创建数据库环境
- 创建包cn.itcast.domain,创建类user
package cn.itcast.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.itcast.dao,创建类UserDao,提供login方法
package cn.itcast.dao;
import cn.itcast.domain.User;
import cn.itcast.util.JDBCUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
//操作数据库中User表
public class UserDao {
//登陆方法
//生命JDBCTemplate对象公用
private JdbcTemplate template=new JdbcTemplate(JDBCUtil.getDataSource());
//loginUser只有用户名和密码
//返回值 包含用户全部数据
public User login(User loginUser){
try{
//编写sql
String sql="Select * from user where username=? and password=?";
//queryForObject可以把返回值封装成一个对象,这里封装成User对象
User user = template.queryForObject(sql,//sql语句
new BeanPropertyRowMapper<User>(User.class),//封装成的对象
loginUser.getUsername(), loginUser.getPasswrod());//查询参数
return user;
}catch (Exception e){//如果查询不到数据,则返回null
e.printStackTrace();//j记录日志
return null;
}
}
}
- 创建JDBCUtil类,使用Durid连接池
package cn.itcast.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;
//jdbc工具类 使用Durid连接池
public class JDBCUtil {
private static DataSource ds;
static{
try{
//加载配置文件,初始化连接池
Properties pro=new Properties();
InputStream resourceAsStream = JDBCUtil.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(resourceAsStream);
//初始化连接池对象
ds=DruidDataSourceFactory.createDataSource(pro);
}catch (Exception e){
e.printStackTrace();
}
}
//获取连接池对象
public static DataSource getDataSource(){
return ds;
}
//获取连接Connection对象
public static Connection getConnection()throws SQLException{
return ds.getConnection();
}
}
测试类UserDaoTest:测试是否好用
package cn.itcast.test;
import cn.itcast.dao.UserDao;
import cn.itcast.domain.User;
import org.junit.Test;
public class UserDaoTest {
@Test
public void testLogin(){
User loginuser=new User();
loginuser.setUsername("zhangsan");
loginuser.setPasswrod("123");
UserDao dao=new UserDao();
User user=dao.login(loginuser);
System.out.println(user);
}
}
- 编写cn.itcast.web.servlet.LoginServlet类,以及登陆成功后转发到的SuccessServlet和失败跳转的FailServlet
package cn.itcast.web;
import cn.itcast.dao.UserDao;
import cn.itcast.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对象
User loginUser=new User();
loginUser.setUsername(username);
loginUser.setPasswrod(password);
//调用UserDao的login方法
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);
}
}
package cn.itcast.web;
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 {
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);
}
}
package cn.itcast.web;
import cn.itcast.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 {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取request域中共享的User
User user = (User) request.getAttribute("user");
if (user!=null){
//给页面写一句话
//设置页面编码
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("登陆成功"+user.getUsername());
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
- login.html中form表单的action写法
- 虚拟目录+servlet资源路径
- BeanUtils工具类,简化数据封装(需要导入jar包)用于封装JavaBean
- 使用的类必须被public修饰
- 必须提供空参构造器
- 成员变量必须使用orivate修饰
- 提供公共setter和getter方法
jar网盘地址,提取码abcd