登录页面

首先导入jar包
在这里插入图片描述在这里插入图片描述

新建两个HTML
login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/req/login" method="post">
    姓名:<input type="text" name="name">
    <br>
   密码:<input type="password" name="password">
    <br>
    <input type="submit" value="提交">
</form>
</body>
</html>

success.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>登录成功</h2>
</body>
</html>

dao包中IUserDao

public interface IUserDao {
    boolean login(Users users);
}

dao–>impl–>UserDaoImpl
public class UserDaoImpl implements IUserDao {
@Override
public boolean login(Users users) {
JdbcTemplate t = new JdbcTemplate(JdbcUtil.getDataSource());
Users users1=null;
try {
users1 = t.queryForObject(“select * from users where name=? and password=?”,
new BeanPropertyRowMapper(Users.class), users.getName(), users.getPassword());
}catch (Exception e){
System.out.println(“查不到”);
return false;
}
return users1!=null;
}
}

User

public class Users {
    private String name;
    private String password;

    @Override
    public String toString() {
        return "Users{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Users() {
    }
}

UserServiceImpl

public class UserServiceImpl implements IUserService {
    private IUserDao dao=new UserDaoImpl();
    @Override
    public boolean login(Users users) {
        return dao.login(users);
    }
}

IUserService

public interface IUserService {
    boolean login(Users users);
}

JdbcUtil

public  class JdbcUtil {

private static DataSource ds;
static {
    Properties prop = new Properties();
    try {
        prop.load(new FileInputStream("D:\\untitled\\yuekao\\src\\druid.properties"));
        ds = new DruidDataSourceFactory().createDataSource(prop);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

    public static DataSource getDataSource() {
        return ds;
    }
public static  <T>T mapToBean(Map map, Class<T> c){
    T t = null;
    try {
        t = c.newInstance();
        BeanUtils.populate(t,map);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return  t;
}

}

LoginServlet

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private IUserService service=new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("UTF-8");
        Map<String, String[]> map = req.getParameterMap();
        Users users = JdbcUtil.mapToBean(map, Users.class);
        boolean isLogin=service.login(users);
        if (isLogin){
            req.getRequestDispatcher("/success.html").forward(req,resp);
        }else {
            req.getRequestDispatcher("/login.html").forward(req,resp);
        }
    }
}

druid.properties

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql:///db3?useSSL=false&serverTimezone=UTC
username=root
password=123456
initialSize=5
maxActive=10
maxWait=3000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值