Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @WebServlet("/controller")
- public class MainServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- private static final String ERROR_PAGE = "error.jsp";
- /**
- * @see HttpServlet#HttpServlet()
- */
- public MainServlet() {
- super();
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- process(request, response);
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- process(request, response);
- }
- private void process(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
- String commandName = request.getParameter("command");
- System.out.println("command: " + commandName);
- Command command = CommandContainer.get(commandName);
- String forward = ERROR_PAGE;
- try {
- forward = command.execute(request, response);
- } catch (Exception ex) {
- request.setAttribute("errorMessage", ex.getMessage());
- }
- // response.sendRedirect(forward);
- request.getRequestDispatcher(forward).forward(request, response);
- }
- }
- public class LoginCommand extends Command {
- private static final long serialVersionUID = -5337459310900087298L;
- private static final String SESSION_ATTR_LOGIN = "login";
- private static final String SESSION_ATTR_ID = "userId";
- private static final String SESSION_ATTR_ROLE = "userRole";
- private static final Logger LOG = Logger.getLogger(LoginCommand.class);
- private static final String PROFILE_JSP = "profile.jsp";
- private static final String USER_INFO_OBJECT="userInfo";
- private UsersService usersService;
- public LoginCommand(UsersService usersService) {
- this.usersService = usersService;
- }
- @Override
- public String execute(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
- HttpSession session = request.getSession();
- String login = request.getParameter("login");
- String password = request.getParameter("password");
- if (login == null || password == null || login.isEmpty() || password.isEmpty()) {
- return null;
- }
- Users user = usersService.getUserByLogin(login);
- if (user == null || !password.equals(user.getPassword())) {
- return null;
- }
- Roles role = new Roles();
- role.setRoleType(RolesType.valueOf(this.usersService.getUserRole(login)));
- String forward = PROFILE_JSP;
- request.setAttribute("user", this.usersService.getUserByLogin(login));
- session.setAttribute(USER_INFO_OBJECT, user);
- session.setAttribute(SESSION_ATTR_LOGIN, login);
- session.setAttribute(SESSION_ATTR_ID, user.getIdUser());
- session.setAttribute(SESSION_ATTR_ROLE, role.getRoleType().toString());
- return forward;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement