Advertisement
Guest User

Untitled

a guest
Aug 15th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. @WebServlet("/controller")
  2. public class MainServlet extends HttpServlet {
  3. private static final long serialVersionUID = 1L;
  4. private static final String ERROR_PAGE = "error.jsp";
  5.  
  6. /**
  7. * @see HttpServlet#HttpServlet()
  8. */
  9. public MainServlet() {
  10. super();
  11.  
  12. }
  13.  
  14.  
  15. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  16. throws ServletException, IOException {
  17. process(request, response);
  18. }
  19.  
  20.  
  21. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  22. throws ServletException, IOException {
  23.  
  24. process(request, response);
  25. }
  26.  
  27.  
  28. private void process(HttpServletRequest request, HttpServletResponse response)
  29. throws IOException, ServletException {
  30.  
  31. String commandName = request.getParameter("command");
  32. System.out.println("command: " + commandName);
  33.  
  34. Command command = CommandContainer.get(commandName);
  35.  
  36. String forward = ERROR_PAGE;
  37. try {
  38. forward = command.execute(request, response);
  39. } catch (Exception ex) {
  40. request.setAttribute("errorMessage", ex.getMessage());
  41.  
  42. }
  43. // response.sendRedirect(forward);
  44. request.getRequestDispatcher(forward).forward(request, response);
  45. }
  46.  
  47. }
  48.  
  49. public class LoginCommand extends Command {
  50.  
  51. private static final long serialVersionUID = -5337459310900087298L;
  52. private static final String SESSION_ATTR_LOGIN = "login";
  53. private static final String SESSION_ATTR_ID = "userId";
  54. private static final String SESSION_ATTR_ROLE = "userRole";
  55. private static final Logger LOG = Logger.getLogger(LoginCommand.class);
  56. private static final String PROFILE_JSP = "profile.jsp";
  57. private static final String USER_INFO_OBJECT="userInfo";
  58. private UsersService usersService;
  59.  
  60. public LoginCommand(UsersService usersService) {
  61. this.usersService = usersService;
  62. }
  63.  
  64. @Override
  65. public String execute(HttpServletRequest request, HttpServletResponse response)
  66. throws IOException, ServletException {
  67. HttpSession session = request.getSession();
  68. String login = request.getParameter("login");
  69. String password = request.getParameter("password");
  70. if (login == null || password == null || login.isEmpty() || password.isEmpty()) {
  71. return null;
  72. }
  73.  
  74. Users user = usersService.getUserByLogin(login);
  75. if (user == null || !password.equals(user.getPassword())) {
  76. return null;
  77. }
  78.  
  79. Roles role = new Roles();
  80. role.setRoleType(RolesType.valueOf(this.usersService.getUserRole(login)));
  81. String forward = PROFILE_JSP;
  82. request.setAttribute("user", this.usersService.getUserByLogin(login));
  83. session.setAttribute(USER_INFO_OBJECT, user);
  84. session.setAttribute(SESSION_ATTR_LOGIN, login);
  85. session.setAttribute(SESSION_ATTR_ID, user.getIdUser());
  86. session.setAttribute(SESSION_ATTR_ROLE, role.getRoleType().toString());
  87.  
  88. return forward;
  89. }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement