0

I want to validate information about user. If user has been entered and trying enter to /login page, forward to userpage.

As of this moment i have this:

StartServlet

public class StartServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserDTO userDTO = (UserDTO) req.getSession().getAttribute("userDTO");
        if (userDTO == null) {
            req.getRequestDispatcher(req.getContextPath() + "/WEB-INF/pages/login.jspx").forward(req, resp);
        } else {
            req.getRequestDispatcher(req.getContextPath() + "/checklogin").forward(req, resp);
        }
    }
}

ValidateServlet

public class ValidateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        UserDTO userDTO = (UserDTO) req.getSession().getAttribute("userDTO");

        if (userDTO == null) {
            userDTO = UserService.getInstance().validateUserDTO(email, password);
        }

        if (userDTO != null) {
            req.getSession().setAttribute("userDTO", userDTO);
            if (userDTO.getRole() == Role.ADMINISTRATOR || userDTO.getRole() == Role.SUPERADMINISTRATOR) {
                req.getRequestDispatcher(req.getContextPath() + "/admin/catalog").forward(req, resp);
            } else {
                req.getRequestDispatcher(req.getContextPath() + "/WEB-INF/pages/user/catalog/catalog.jspx").forward(req, resp);
            }
        } else {
            String errorMsg = "Email or password is incorrect";
            req.setAttribute("error", errorMsg);
            req.getRequestDispatcher(req.getContextPath() + "/WEB-INF/pages/login.jspx").forward(req, resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

AdminCatalogServlet

public class AdminCatalogServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<ItemDTO> itemsDTO = ItemService.getInstance().getAllItemsDTO();
        req.setAttribute("itemsDTO", itemsDTO);
        req.getRequestDispatcher(req.getContextPath() + "/WEB-INF/pages/admin/catalog/catalog.jspx").forward(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

web.xml

<servlet>
        <servlet-name>StartServlet</servlet-name>
        <servlet-class>...StartServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>StartServlet</servlet-name>
        <url-pattern>/</url-pattern>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>ValidateServlet</servlet-name>
        <servlet-class>...ValidateServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ValidateServlet</servlet-name>
        <url-pattern>/checklogin</url-pattern>
    </servlet-mapping>
<servlet>
        <servlet-name>AdminCatalogServlet</servlet-name>
        <servlet-class>...AdminCatalogServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>AdminCatalogServlet</servlet-name>
        <url-pattern>/admin/catalog</url-pattern>
    </servlet-mapping>

Problem: We starting from StartServlet, when calling / or /login. Then we enter login, password to form (method = POST) and submit to ValidateServlet.

After it we forward to AdminCatalogServlet (from prospective of Admin). Then we forward to jsp and our browser show to us page. (It was 2 requests to the database) for user and for items. I think, that is all, but...then tomcat run again method doGet() in StartServlet, take user from session and forward to ValidateServlet to method Get. And repeated request to the database for items.

Why is this happening? Thanks.

UPDATE: If delete doGet() method from ValidateServlet or AdminCatalogServlet, the problem goes away, but then don't work when trying to enter to /login when you have logged in.

UPDATE2: I have traced, as advised in the comments (maybe not right). First the first request (POST), then the set of additional data of bootstrap. At the end there is a second request on /validate with the GET method.

UPDATE2: To solve this problem, I have changed the StartServlet mapping from / to "". This means that any request to the host:port/ will be redirected to this servlet. The problem was due to the fact that, since the Bootstrap was connected, the browser also sent a request for favicon, css and etc. to the host:port/favicon. Because the StartServlet was mapped on /, then it received this request and method GET was started. For more information you can read about DefaultServlet in Tomcat

AlexBal
  • 1
  • 2
  • You probably want to track all the requests that you are actually doing in a browser (Dev Tools -> Network, and enable "Preserve Log" check box). – Sergei Sirik Jul 27 '17 at 23:29
  • _"don't work when trying to enter to /login when you have logged in"_ I assume, It doesn't work because this line in `StartServlet` `req.getRequestDispatcher(req.getContextPath() + "/checklogin").forward(req, resp);` will pass you to `ValidateServlet.doGet` – Sergei Sirik Jul 27 '17 at 23:49
  • Thanks, but I understand why it doesn't work. :) The question was asked the other. – AlexBal Jul 28 '17 at 07:11

0 Answers0