Guest User

Untitled

a guest
Feb 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. package main;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.HashMap;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.Cookie;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import javax.servlet.http.HttpSession;
  12. import model.User;
  13.  
  14. /**
  15.  *
  16.  * @author ahmedkotb
  17.  */
  18. public class Login extends HttpServlet {
  19.  
  20.     /**
  21.      * Processes requests for both HTTP <code>GET</code> and
  22.      * <code>POST</code> methods.
  23.      * @param request servlet request
  24.      * @param response servlet response
  25.      * @throws ServletException if a servlet-specific error occurs
  26.      * @throws IOException if an I/O error occurs
  27.      */
  28.     protected void processRequest(HttpServletRequest request,
  29.             HttpServletResponse response)
  30.             throws ServletException, IOException {
  31.         response.setContentType("text/html;charset=UTF-8");
  32.         PrintWriter out = response.getWriter();
  33.        
  34.         HttpSession session = request.getSession();
  35.        
  36.         String sessionUsername = (String) session.getAttribute("username");
  37.        
  38.         if (sessionUsername != null) {
  39.             response.sendRedirect("/welcome.jsp");
  40.         }
  41.        
  42.     String username = request.getParameter("username");
  43.     String password = request.getParameter("password");
  44.        
  45.         User user = new User(request.getParameter("username"),
  46.                             request.getParameter("password"));
  47.        
  48.        
  49.     if (user.login()) {
  50.             session.setAttribute("username", username);
  51.             Cookie cookieUsername = new Cookie("username", username);
  52.             Cookie cookiePassword = new Cookie("password", password);
  53.             cookieUsername.setMaxAge(120);
  54.             cookiePassword.setMaxAge(120);
  55.             response.addCookie(cookieUsername);
  56.             response.addCookie(cookiePassword);
  57.            
  58.            
  59.             response.sendRedirect("/welcome.jsp");
  60.     }else{
  61.             System.out.println("Hello");
  62.         request.setAttribute("error_msg","The username or password you "
  63.                     + "entered is incorrect." );
  64.             getServletContext().getRequestDispatcher("/LoginForm.jsp")
  65.                     .forward(request, response);
  66.     }
  67.     }
  68.  
  69.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods.
  70.     // Click on the + sign on the left to edit the code.">
  71.     /**
  72.      * Handles the HTTP <code>GET</code> method.
  73.      * @param request servlet request
  74.      * @param response servlet response
  75.      * @throws ServletException if a servlet-specific error occurs
  76.      * @throws IOException if an I/O error occurs
  77.      */
  78.     @Override
  79.     protected void doGet(HttpServletRequest request,
  80.             HttpServletResponse response)
  81.             throws ServletException, IOException {
  82.         processRequest(request, response);
  83.     }
  84.  
  85.     /**
  86.      * Handles the HTTP <code>POST</code> method.
  87.      * @param request servlet request
  88.      * @param response servlet response
  89.      * @throws ServletException if a servlet-specific error occurs
  90.      * @throws IOException if an I/O error occurs
  91.      */
  92.     @Override
  93.     protected void doPost(HttpServletRequest request,
  94.             HttpServletResponse response)
  95.             throws ServletException, IOException {
  96.         processRequest(request, response);
  97.     }
  98.  
  99.     /**
  100.      * Returns a short description of the servlet.
  101.      * @return a String containing servlet description
  102.      */
  103.     @Override
  104.     public String getServletInfo() {
  105.         return "Short description";
  106.     }// </editor-fold>
  107. }
Add Comment
Please, Sign In to add comment