项目要求:
使用Servlet+Druid+JdbcTemplate实现用户登录功能,后台为Mysql数据库
1.编写静态登录页面login.html,页面中包含:姓名,密码
2.编写处理登录功能的Servlet,使用注解版或配置版
3.使用MVC三层架构,编写Service层和Dao层,要有接口和实现类
4.在Dao层中使用Druid连接池和JdbcTemplate查询Mysql中的Users表数据,判断登录是否成功
5.要求编写druid.properties文件存放jdbc参数,编写JDBC工具类获取数据源DataSource
6.如果登录成功,跳转到success.html,提示:登录成功,欢迎您!
7.如果登录失败,继续跳转到登录页面。
8.登录页面需要使用js或jq进行表单验证:如果用户名或密码为空,则不允许提交,并在对应的输入框后面显示红色文字提示:用户名为空,或密码为空!
效果展示:
login.html页面:
<form action="login" method="post" onsubmit="return funx()">
用户名:<input type="text" id="name" name="name"><span id="span1"></span><br>
密码:<input type="password" name="password" id="password"><span id="span2"></span><br>
<input type="submit" value="提交">
</form>
</body>
<script>
function funx() {
let name = document.getElementById("name").value;
let password = document.getElementById("password").value;
let span1 = document.getElementById("span1");
let span2 = document.getElementById("span2");
console.log(name);
console.log(password);
if (name==""||name==null||(password==""||password==null)){
if (name==""||name==null||(password==""||password==null)){
if (name==""||name==null){
span1.innerText='用户名不能为空';
span1.style.color="red";
}
if (password==""||password==null){
span2.innerText="密码不能为空";
span2.style.color="red";
}
return false
}else {
return true
}
}
}
</script>
success.html页面:
<body>
<h2>恭喜您,登录成功!</h2>
</body>
druid.properties文件:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/long?useSSL=false&serverTimezone=UTC
username=root
password=123456
工具类:
public class JdbcUtil {
public static DataSource getDataSource(){
Properties prop = new Properties();
DataSource ds = null;
try {
prop.load(new FileInputStream("D:\\IDEA_HOME_SERVLET\\exam01\\src\\druid.properties"));
ds = DruidDataSourceFactory.createDataSource(prop);
} catch (Exception e) {
e.printStackTrace();
}
return ds;
}
}
entity层:
package login.entity;
public class Users {
private String name;
private String 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;
}
@Override
public String toString() {
return "Users{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
public Users() {
}
public Users(String name, String password) {
this.name = name;
this.password = password;
}
}
dao层:
public interface UserDao {
boolean checkUser(Users users);
}
dao的实现类:
public class UserDaoImpl implements UserDao {
@Override
public boolean checkUser(Users users) {
DataSource ds = JdbcUtil.getDataSource();
JdbcTemplate template = new JdbcTemplate(ds);
List<Users> query = template.query("select * from users where name=? and psw=?",
new BeanPropertyRowMapper<>(Users.class), users.getName(), users.getPassword());
if (query.size()==0){
return false;
}else {
return true;
}
}
}
service层:
public interface UserService {
boolean checkUser(Users users);
}
service的实现类:
public class UserServiceImpl implements UserService {
@Override
public boolean checkUser(Users users) {
UserDaoImpl userDao = new UserDaoImpl();
return userDao.checkUser(users);
}
}
servlet层:
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
req.setCharacterEncoding("utf-8");
System.out.println("登录中心");
//获取到浏览器的数据
String name = req.getParameter("name");
String password = req.getParameter("password");
UserServiceImpl service = new UserServiceImpl();
boolean b = service.checkUser(new Users(name, password));
if (b){
//如果数据库存在要查找的数据,就为true,转发到success.html页面中
req.getRequestDispatcher("/success.html").forward(req,resp);
}else {
//如果不存在,就转发到登录界面login.html
req.getRequestDispatcher("/login.html").forward(req,resp);
}
}
}