package Servlet;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import beans.User;
import DAO.JDBCUserDAO;
/**
* Servlet implementation class Login
*/
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Login() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("out").equals("true")){
request.getSession().setAttribute("user", null);
request.getRequestDispatcher("index.jsp").forward(request, response);
return;
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
JDBCUserDAO userDAO = new JDBCUserDAO();
if(userDAO.checkIfExistAndValidate(request.getParameter("login"), request.getParameter("pwd"))){
User user =userDAO.getUser(request.getParameter("login"));
request.getSession().setAttribute("user",user);
if(user.getDateFinAbonnementFormatDate()!=null){
if(user.getDateFinAbonnementFormatDate().compareTo(new Date()) > 0){
request.getSession().setAttribute("allowed","true");
}
}
response.sendRedirect(response.encodeURL("index.jsp"));
return;
}else{
request.setAttribute("error", "Wrong login and/or password");
request.getRequestDispatcher("login.jsp").forward(request, response);
return;
}
}
}