叨叨两句
- ~
Filter
自动登陆
- 登陆页面填写信息,提交后跳转至loginServlet
- 调用service方法处理登陆业务
- 判断返回是否为空,若不为空,则说明登陆成功
- 若是登陆成功,判断是否须要自动登陆
- 若是须要就将用户名和密码存储到cookie中,并经过response发送客户端
- filter到cookie中获取用户名和密码
- 调用service的方法,根据用户名和密码查询user对象
- 将user对象存储到session中
- 放行
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1.得到用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//2.调用业务,进行登陆
LoginService loginService = new LoginService();
User user = loginService.login(username,password);
//3.判断用户是否为空
if(user != null){
//登陆成功
//response.getWriter().print("Login Success...");
String auto = request.getParameter("auto");
if(auto != null && "ok".equalsIgnoreCase(auto)){
System.out.println("用户勾选了自动登陆...");
Cookie cookie = new Cookie("autoLogin", username+"-"+password);
cookie.setPath(request.getContextPath());
cookie.setMaxAge(60*60*24);
response.addCookie(cookie);
}
request.getSession().setAttribute("user", user);
response.sendRedirect(request.getContextPath()+"/WEB01/index.jsp");
}else{
//登陆失败
response.getWriter().print("Login Fail...");
}
} catch (SQLException e) {
e.printStackTrace();
//登陆失败
response.getWriter().print("Login Fail...");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}