package login;


import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/UserLoginAction")
public class UserLoginAction extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse response) throws IOException {
        String path = req.getContextPath();
        if (req.getSession().getAttribute("user") != null){
            response.sendRedirect(path+"/home.jsp");
        }
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        UserInfo user = UserDB.GetUserInfoByName(username);
        if(user == null) {
            response.sendRedirect(path+"/login/login.jsp&loginflag=1");
        }else if (user.getPass().equals(password)) {
            req.getSession().setAttribute("user", user);
            response.sendRedirect(path+"/home.jsp");
        }else{
            response.sendRedirect(path+"/login/login.jsp&loginflag=2");
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect("error.jsp");
    }
}
