表现层
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
private UserService userService = new UserService();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
Map<String, String[]> parameterMap = request.getParameterMap();
User user = new User();
try {
BeanUtils.populate(user, parameterMap);
} catch (Exception e) {
e.printStackTrace();
}
User user1 = userService.selectUser(user);
if (user1 != null) {
// 记住登录状态
// 获取session对象
HttpSession session = request.getSession();
// 将user对象加入session域
// 数据存入服务端
session.setAttribute("user", user);
String remember = request.getParameter("remember");
// 获取cookie对象,将键和值存入cookies
Cookie username = new Cookie("username", user.getUsername());
Cookie password = new Cookie("password", user.getPassword());
if ("1".equals(remember)) {
// 设置cookie对象的存活时间
username.setMaxAge(60 * 60 * 24 * 7);
password.setMaxAge(60 * 60 * 24 * 7);
// 发送cookie到客户端
response.addCookie(username);
response.addCookie(password);
}else {
// 参数是0表示删除cookie
username.setMaxAge(0);
password.setMaxAge(0);
response.addCookie(username);
response.addCookie(password);
}
// 如果登录成功,将跳转到另一个表现层查询数据
response.sendRedirect(request.getContextPath() + "/selectAllServlet");
} else {
// 将值传入request域,用来发送给前端,提示用户名密码错误
request.setAttribute("msg", "用户名或密码不正确");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
业务逻辑层
public class UserService {
public User selectUser(User user) {
SqlSessionFactory Factory = SqlSessionFactoryUtils.getSqlSessionFactory();
SqlSession sqlSession = Factory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user1 = mapper.selectUser(user);
sqlSession.close();
return user1;
}
}
数据访问层
public interface UserMapper {
User selectUser(User user);
}
映射配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cyf.mapper.UserMapper">
<!--<resultMap id="" type="">-->
<!--</resultMap>-->
<select id="selectUser" resultType="com.cyf.pojo.User">
select *
from tb_user where
username = #{username}
and password = #{password}
</select>
</mapper>
前端页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
<link href="css/login.css" rel="stylesheet">
</head>
<body>
<div id="loginDiv" style="height: 350px">
<form action="/cookieSession_demo/UserServlet" id="form">
<h1 id="loginMsg">LOGIN IN</h1>
<div id="errorMsg">${msg}</div>
<%--记住用户名和密码--%>
<%--可以从cookie中将数据和密码取出--%>
<p>Username:<input id="username" name="username" value="${cookie.username.value}" type="text"></p>
<p>Password:<input id="password" name="password" value="${cookie.password.value}" type="password"></p>
<p>Remember:
<%--判断如果cookie中是否有数据--%>
<%--如果没有数据的话说明用户没有选择了记住用户名和密码--%>
<%--所以要取消单选按钮选中--%>
<c:if test="${cookie.password.value == null }">
<input id="remember" name="remember" type="checkbox" value="1"></p>
</c:if>
<%--如果有数据的话说明用户选择了记住用户名和密码--%>
<%--所以要将单选按钮选中--%>
<c:if test="${cookie.password.value != null }">
<input id="remember" name="remember" type="checkbox" value="1" checked></p>
</c:if>
<div id="subDiv">
<input type="submit" class="button" value="login up">
<input type="reset" class="button" value="reset">
<a href="register.html">没有账号?</a>
</div>
</form>
</div>
</body>
</html>
如果用户登录成功,将在登录后页面顶部显示***,欢迎您!
<h1>${user.username},欢迎您!</h1>