package login;


import jakarta.servlet.ServletException;
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 ServletException, 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");
        int flag;
        UserInfo user = UserDB.GetUserInfoByName(username);
        if(user == null) {
            flag = 1;
        }else if (user.getPass().equals(password)) {
            flag=0;
            req.getSession().setAttribute("user", user);
        }else{
            flag=2;
        }
        System.out.println(user);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.sendRedirect("error.jsp");
    }
}
