package cn.itcast.heimaPro.web.Servlet;
import cn.itcast.heimaPro.domain.Manager;
import cn.itcast.heimaPro.domain.User;
import cn.itcast.heimaPro.service.ManagerService;
import cn.itcast.heimaPro.service.ManagerServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/managerLoginServlet")
public class ManagerLoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String code = request.getParameter("verifycode");//输入的验证码
HttpSession session = request.getSession();
String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");//生成的验证码
session.removeAttribute("CHECKCODE_SERVER");//保证验证码的一次性,不会重复
if (!code.equalsIgnoreCase(checkcode_server)) {
request.setAttribute("login_msg", "验证码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
return;
}
String username = request.getParameter("username");
String password = request.getParameter("password");
Manager manager = new Manager();
manager.setUsername(username);
manager.setPassword(password);
session.setAttribute("manager", manager);
//5.调用Service查询
ManagerService service = new ManagerServiceImpl();
Manager loginManager = service.login(manager);
if (loginManager == null) {
request.setAttribute("login_msg", "用户名或密码错误");
request.getRequestDispatcher(request.getContextPath() + "/login.jsp").forward(request, response);
} else if (loginManager != null) {
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
System.out.println(username);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
} |
|