第五节 搭建环境:辅助功能
1、常量类
![images](https://i-blog.csdnimg.cn/blog_migrate/7b7c071a30176eaab8f7169ead3ad02d.png)
public class ImperialCourtConst {
public static final String LOGIN_FAILED_MESSAGE = "账号、密码错误,不可进宫!";
public static final String ACCESS_DENIED_MESSAGE = "宫闱禁地,不得擅入!";
}
2、MD5 加密工具方法
![images](https://i-blog.csdnimg.cn/blog_migrate/32f914fe8769c3e7bcb00fad3f74ccac.png)
public class MD5Util {
public static String encode(String source) {
if (source == null || "".equals(source)) {
throw new RuntimeException("用于加密的明文不可为空");
}
String algorithm = "md5";
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte[] input = source.getBytes();
byte[] output = messageDigest.digest(input);
int signum = 1;
BigInteger bigInteger = new BigInteger(signum, output);
int radix = 16;
String encoded = bigInteger.toString(radix).toUpperCase();
return encoded;
}
}
3、日志配置文件
![images](https://i-blog.csdnimg.cn/blog_migrate/08061537e3966513d5c0a31942defbd7.png)
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
<logger name="com.atguigu" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
</configuration>
第六节 业务功能:登录
1、显示首页
①流程图
![images](https://i-blog.csdnimg.cn/blog_migrate/f72255383cfefcf8b47c91d418853ad2.png)
②创建 PortalServlet
[1]创建 Java 类
![images](https://i-blog.csdnimg.cn/blog_migrate/3b957c8f6de8aa0c97e72144456605bf.png)
public class PortalServlet extends ViewBaseServlet {
@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 templateName = "index";
processTemplate(templateName, req, resp);
}
}
[2]注册
![images](https://i-blog.csdnimg.cn/blog_migrate/8b5bcc049f4fd0700d5bb10bed7590c5.png)
<servlet>
<servlet-name>portalServlet</servlet-name>
<servlet-class>com.atguigu.imperial.court.servlet.module.PortalServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>portalServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
③在 index.html 中编写登录表单
![iamgs](https://i-blog.csdnimg.cn/blog_migrate/44743267b23096e06145c060cb647b02.png)
<!DOCTYPE html>
<html lang="en" xml:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/auth}" method="post">
<input type="hidden" name="method" value="login" />
<p th:text="${message}"></p>
<p th:text="${systemMessage}"></p>
账号:<input type="text" name="loginAccount"/><br/>
密码:<input type="password" name="loginPassword"><br/>
<button type="submit">进宫</button>
</form>
</body>
</html>
2、登录操作
①流程图
![images](https://i-blog.csdnimg.cn/blog_migrate/47478ea0991e5053221dbb75e8439776.png)
②创建 EmpService
![images](https://i-blog.csdnimg.cn/blog_migrate/de3585f33273237a48c06be601462c07.png)
③创建登录失败异常
![images](https://i-blog.csdnimg.cn/blog_migrate/4db2941f447a38b3fde916f0190b40cb.png)
public class LoginFailedException extends RuntimeException {
public LoginFailedException() {
}
public LoginFailedException(String message) {
super(message);
}
public LoginFailedException(String message, Throwable cause) {
super(message, cause);
}
public LoginFailedException(Throwable cause) {
super(cause);
}
public LoginFailedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
④增加常量声明
![images](https://i-blog.csdnimg.cn/blog_migrate/0e0ccb38a8a9dc20ece10a1eee3b5b7b.png)
public class ImperialCourtConst {
public static final String LOGIN_FAILED_MESSAGE = "账号、密码错误,不可进宫!";
public static final String ACCESS_DENIED_MESSAGE = "宫闱禁地,不得擅入!";
public static final String LOGIN_EMP_ATTR_NAME = "loginInfo";
}
⑤创建 AuthServlet
[1]创建 Java 类
![images](https://i-blog.csdnimg.cn/blog_migrate/6692357d835a4f2f086d6825c1426ecb.png)
public class AuthServlet extends ModelBaseServlet {
private EmpService empService = new EmpServiceImpl();
protected void login(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
String loginAccount = request.getParameter("loginAccount");
String loginPassword = request.getParameter("loginPassword");
Emp emp = empService.getEmpByLoginAccount(loginAccount, loginPassword);
HttpSession session = request.getSession();
session.setAttribute(ImperialCourtConst.LOGIN_EMP_ATTR_NAME, emp);
String templateName = "temp";
processTemplate(templateName, request, response);
} catch (Exception e) {
e.printStackTrace();
if (e instanceof LoginFailedException) {
request.setAttribute("message", e.getMessage());
processTemplate("index", request, response);
}else {
throw new RuntimeException(e);
}
}
}
}
[2]注册
![images](https://i-blog.csdnimg.cn/blog_migrate/facf46f2aa46b6629579e803c32896e7.png)
<servlet>
<servlet-name>authServlet</servlet-name>
<servlet-class>com.atguigu.imperial.court.servlet.module.AuthServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>authServlet</servlet-name>
<url-pattern>/auth</url-pattern>
</servlet-mapping>
⑥EmpService 方法
![images](https://i-blog.csdnimg.cn/blog_migrate/3da7137f4178d253920d81f2787f3d55.png)
public class EmpServiceImpl implements EmpService {
private EmpDao empDao = new EmpDaoImpl();
@Override
public Emp getEmpByLoginAccount(String loginAccount, String loginPassword) {
String encodedLoginPassword = MD5Util.encode(loginPassword);
Emp emp = empDao.selectEmpByLoginAccount(loginAccount, encodedLoginPassword);
if (emp != null) {
return emp;
} else {
throw new LoginFailedException(ImperialCourtConst.LOGIN_FAILED_MESSAGE);
}
}
}
⑦EmpDao 方法
![images](https://i-blog.csdnimg.cn/blog_migrate/22d3b30aa1e88e76e6dfebbf05d64593.png)
public class EmpDaoImpl extends BaseDao<Emp> implements EmpDao {
@Override
public Emp selectEmpByLoginAccount(String loginAccount, String encodedLoginPassword) {
String sql = "select emp_id empId," +
"emp_name empName," +
"emp_position empPosition," +
"login_account loginAccount," +
"login_password loginPassword " +
"from t_emp where login_account=? and login_password=?";
return super.getSingleBean(sql, Emp.class, loginAccount, encodedLoginPassword);
}
}
⑧临时页面
![images](https://i-blog.csdnimg.cn/blog_migrate/ab62df9bf74cf869c9f47c46f5cb2fb9.png)
<!DOCTYPE html>
<html lang="en" xml:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>临时</title>
</head>
<body>
<p th:text="${session.loginInfo}"></p>
</body>
</html>
3、退出登录
①在临时页面编写超链接
![images](https://i-blog.csdnimg.cn/blog_migrate/a8aaa1722e160b18e416366a86224eee.png)
<a th:href="@{/auth?method=logout}">退朝</a>
②在 AuthServlet 编写退出逻辑
![images](https://i-blog.csdnimg.cn/blog_migrate/a5c8b8fdf5db468dc94374f2d86f0383.png)
protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.invalidate();
String templateName = "index";
processTemplate(templateName, request, response);
}