package servlet.account;

import java.io.IOException;
import java.sql.SQLException;

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 core.user.Student;
import core.user.User;
import core.user.utils.AccountManagement;
import error.GExcptAccount;
import error.GExcptSQL;

@WebServlet("/login")
public class S_Login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String id=request.getParameter("id");
        String password=request.getParameter("password");
        User user;
        try {
            user = AccountManagement.login(id,password);
        } catch (GExcptSQL | SQLException gExcptSQL) {
            gExcptSQL.printStackTrace();
            response.sendRedirect("/op_fail.jsp");
            return;
        } catch (GExcptAccount gExcptAccount) {
            gExcptAccount.printStackTrace();
            response.sendRedirect("/op_fail.jsp");
            return;
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("/op_fail.jsp");
            return;
        }
        request.getSession().setAttribute("user",user);
        response.sendRedirect("/home/home.jsp");
        return;
    }
}