需求:表单登录,对用户名和密码进行验证,并给出响应
思路
这里使用MVC设计模式
- M-存取数据的对象,这里使用Javabean
- V-展示,这里使用html实现
- C-控制器,这里使用servlet实现
- 为了更好的实现分离,将servlet细分为处理业务逻辑的service和处理数据的dao层
代码
存储数据的Javabean
package myday20201110.login.javaBean;
import lombok.Data;
//存储数据的Javabean
@Data
public class User {
private String username;
private String password;
}
自定义异常
package myday20201110.login.exception;
//自定义异常
public class LoginException extends Exception {
public LoginException(String message) {
super(message);
}
}
dao层,操作数据
package myday20201110.login.dao;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import myday20201110.login.javaBean.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import java.sql.SQLException;
//dao层,操作数据
public class LoginDao {
//根据username查找user
public User getByUsername(String username) {
User user = null;
QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource());
String sql="SELECT * FROM loginuser WHERE username=?";
Object[] parms={username};
try {
user = queryRunner.query(sql, new BeanHandler<User>(User.class),parms);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return user;
}
}
service层,业务逻辑处理,判断用户名和密码是否正确
package myday20201110.login.service;
import myday20201110.login.dao.LoginDao;
import myday20201110.login.exception.LoginException;
import myday20201110.login.javaBean.User;
//service层,业务逻辑处理,判断用户名和密码是否正确
public class LoginService {
LoginDao loginDao=new LoginDao();
public void login(User user) throws LoginException {
String username = user.getUsername();
User username1 = loginDao.getByUsername(username);
if (username1==null){
throw new LoginException("用户不存在");
}
if (!user.getPassword().equals(username1.getPassword())){
throw new LoginException("密码不正确");
}
}
}
controller层 接收请求并响应
package myday20201110.login.controller;
import myday20201110.login.exception.LoginException;
import myday20201110.login.javaBean.User;
import myday20201110.login.service.LoginService;
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;
//controller层 接收请求并响应
@WebServlet("/loginservlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
User user = new User();
user.setUsername(username);
user.setPassword(password);
LoginService loginService = new LoginService();
try {
// 如果没有异常,说明成功,重定向到登录成功界面
loginService.login(user);
resp.sendRedirect("/successed.html");//重定向,算两次请求
} catch (LoginException e) {
// 如果有异常,捕获异常内容,添加到req的属性中,并转发到原登录界面,给出提示
String message = e.getMessage();
req.setAttribute("msg",message);
req.getRequestDispatcher("/login.jsp").forward(req,resp);//转发,算一次请求,带额外信息
}
}
}
登录界面
<%--
Created by IntelliJ IDEA.
User: 17621802479
Date: 2020/11/10
Time: 16:12
To change this template use File | Settings | File Templates.
--%>
<%--登录界面--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
<%
//获取属性值,也就是异常信息
String msg = (String) request.getAttribute("msg");
if (msg==null){
msg="";
}
%>
</head>
<body>
<form method="post" action="/loginservlet">
<%--添加信息到原注册界面 给出提示信息 <%=msg%>注意等号--%>
用户名<input name="username" type="text"/><span style="color: aqua"><%=msg%></span><br>
密码<input name="password" type="password"/><br>
<input type="submit" value="登录"/>
</form>
</body>
</html>
成功界面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<!--成功界面-->
<body>
<h2>成功啦</h2>
<a href="http://www.baidu.com">进入百度</a>
</body>
</html>
测试