SHARE
TWEET

AuthorizationServlet

a guest Jun 5th, 2017 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.computerScience.servlets;
  2.  
  3. import com.computerScience.addition.MD5;
  4. import com.computerScience.dto.UserDTO;
  5. import com.computerScience.service.impl.UserServiceImpl;
  6. import com.computerScience.util.EntityConstants;
  7. import com.computerScience.util.View;
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.annotation.WebServlet;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import javax.servlet.http.HttpSession;
  15. import java.io.IOException;
  16. import java.util.List;
  17.  
  18. /**
  19.  * Created by Sky_el on 01.06.2017.
  20.  */
  21. @WebServlet(View.Mapping.LOGIN)
  22. public class AuthorizationServlet extends HttpServlet {
  23.     private final MD5 md5 = new MD5();
  24.     private final UserServiceImpl userService = new UserServiceImpl();
  25.  
  26.     @Override
  27.     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  28.         String login = req.getParameter(EntityConstants.EMAIL_PARAM);
  29.         String password = req.getParameter(EntityConstants.PASSWORD_PARAM);
  30.  
  31.         List<UserDTO> userList = userService.getUsersList();
  32.         UserDTO foundUser;
  33.  
  34.         if((foundUser = searchInAccountList(userList, login, md5.md5Custom(password))) != null) {
  35.             HttpSession session = req.getSession();
  36.             session.setAttribute(EntityConstants.USER_SESSION_ATTR, foundUser);
  37.             resp.sendRedirect(String.format("%s%s", req.getContextPath(), View.Mapping.MAIN_PAGE));
  38.         }
  39.         else
  40.         {
  41.             req.setAttribute(EntityConstants.NOT_FOUND_ERROR_ATTR, true);
  42.             req.getRequestDispatcher(View.MAIN_JSP).forward(req, resp); // СДЕЛАТЬ РЕДИРЕКТ НА СТРАНИЦУ, ОТ КУДА БЫЛ ВЫПОЛНЕН ЗАПРОС
  43.         }
  44.     }
  45.  
  46.     private UserDTO searchInAccountList(List<UserDTO> userList, String email, String password) {
  47.         for(UserDTO user : userList) {
  48.             if(user.getEmail().equals(email) && user.getPassword().equals(password))
  49.                 return user;
  50.         }
  51.         return null;
  52.     }
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top