第五节 搭建环境:辅助功能
1、常量类
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/58a0d5489c8f8231e43099e9c5822e51.png#pic_center)
public class ImperialCourtConst {
public static final String LOGIN_FAILED_MESSAGE = "账号、密码错误,不可进宫!";
public static final String ACCESS_DENIED_MESSAGE = "宫闱禁地,不得擅入!";
}
2、MD5 加密工具方法
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/41023b5b2bbd3afa3535d590d2bc4c54.png#pic_center)
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、日志配置文件
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/90497ab76d6fd14956e9d3b8f94df315.png#pic_center)
<?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、显示首页
①流程图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7176509ade90d8826111a858dc8763ee.png#pic_center)
②创建 PortalServlet
[1]创建 Java 类
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/be2f0b288a4dca04599a0d41bf8203d1.png#pic_center)
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]注册
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d310d448f110ace90b67eb17960e9b13.png#pic_center)
<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 中编写登录表单
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5322ac6733d1f5d117886a31fb176bd5.png#pic_center)
<!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、登录操作
①流程图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c8dc39ca8ad200e66be45a0a6ed3a5b6.png#pic_center)
②创建 EmpService
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cd843cf0c5691904f53f0124e1999f0f.png#pic_center)
③创建登录失败异常
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f2263a6c97805339bb901488ce2b40d1.png#pic_center)
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);
}
}
④增加常量声明
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b77e0db1e395a8730694446ab3c4e6b4.png#pic_center)
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 类
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3f946c6e5d61541d0fa201892a1c4536.png#pic_center)
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]注册
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/facc7acb90a348715147b7387a46bdbf.png#pic_center)
<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 方法
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0972047178b66a5a1093aa06fc25862c.png#pic_center)
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 方法
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a48c52a2ac7b8584bf1af12fa3066624.png#pic_center)
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);
}
}
⑧临时页面
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/efcb235e5eaca9ddec6ab18aea86bb2f.png#pic_center)
<!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、退出登录
①在临时页面编写超链接
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/614bbdce0dbd4cd2eb1599ba232c8ba4.png#pic_center)
<a th:href="@{/auth?method=logout}">退朝</a>
②在 AuthServlet 编写退出逻辑
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6a74312a6c0294b245c2ddd5b9548722.png#pic_center)
protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.invalidate();
String templateName = "index";
processTemplate(templateName, request, response);
}