package controller;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import domain.User;
import model.UserModel;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
try {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
user.setUsername(username);
user.setPassword(password);
String checkcode1 = request.getParameter("checkckcode");
String checkcode2 = (String) request.getSession().getAttribute("checkcode");
request.getSession().removeAttribute("checkcode");
if(!checkcode1.equalsIgnoreCase(checkcode2)) {
request.setAttribute("msg", "验证码输入错误!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
return;
}
UserModel userModel = new UserModel();
User existUser = userModel.login(user);
if(existUser == null) {
//登录失败
request.setAttribute("msg", "用户名或密码错误!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}else {
//登录成功
HttpSession session = request.getSession();
session.setAttribute("existUser", existUser);
String remember = request.getParameter("remember");
if("true".equals(remember)) {
Cookie cookie = new Cookie("remember",existUser.getUsername());
cookie.setPath("/web03");
cookie.setMaxAge(60*60*24);
response.addCookie(cookie);
}
response.sendRedirect("/web03/success.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
throw new Runtime