request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String name = request.getParameter("username");
String password = request.getParameter("password");
String remember = request.getParameter("remember");
System.out.println(name);
Admin am = (Admin) as.selectadmin(name, password);
System.out.println(am);
if(name.equals("")&&password.equals("")){
// login1msg
request.setAttribute("login1msg","用户名与密码不能为空");
request.getRequestDispatcher("/jsp/managerlogin.jsp").forward(request, response);
}
else{//用户名与密码不为空
if (am==null) {
String login1msg = "用户名或密码错误";
request.setAttribute("login1msg", login1msg);
System.out.println(login1msg);
request.getRequestDispatcher("/jsp/managerlogin.jsp").forward(request, response);
System.out.println("失败");
} else {
//登录成功
if ("1".equals(remember)) {//记住用户密码
Cookie ck1 = new Cookie("name", name);
Cookie ck2 = new Cookie("password", password);
ck1.setMaxAge(60 * 60 * 24 * 7);
ck2.setMaxAge(60 * 60 * 24 * 7);
response.addCookie(ck1);
response.addCookie(ck2);
} else {//不记住用户密码
Cookie[] cks = request.getCookies();
if (cks != null) {
for (Cookie c : cks) {
if (c.getName().equals("name") || c.getName().equals("password")) {
c.setMaxAge(-1);
response.addCookie(c);
}
}
}
}
HttpSession session = request.getSession();//获取session 对象
session.setAttribute("am", am);//将新登录的用户放入session中,在项目最后通过过滤器判断是否已经登陆
response.sendRedirect(request.getContextPath() + "/jsp/index.jsp");//
}
}