Get method of http being automatically invoked

2

Good morning, I'm implementing the get servlet method here with a very simple logic.

If the user passes a url in the default: "... / entities / id", the edit form should already be opened with the entity corresponding to the id typed after the slash. But if the url's default is: "... / entities" the list of all entities will be loaded and the entity listing page will open.

Everything is working, but with a problem. After the last line is executed, it goes back to the beginning of the get method, as if something were invoking the get method, so it gets infinite loop.

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    String requestUri = req.getRequestURI();
    Long id = RegexUtil.matchId(requestUri);

    if (id != null) {
        // Informou um id
        Entidade entidade = entidadeService.getEntidadeById(id);

        if (entidade != null) {

            req.setAttribute("objEntidade", entidade);                
            req.getRequestDispatcher("paginas/cadastroentidades.jsp").forward(req, resp);
            // Após executar esta linha, volta automaticamente para a primeira linha do método
        } else {
            resp.sendError(404, "Entidade não encontrada");
        }

    } else {

        List<Entidade> lstEntidades = entidadeService.getEntidades();

        req.setAttribute("lstEntidades", lstEntidades);            
        req.getRequestDispatcher("paginas/listaentidades.jsp").forward(req, resp);
        // Após executar esta linha, volta automaticamente para a primeira linha do método
    }
}

Any help is welcome.

    
asked by anonymous 05.07.2016 / 11:40

1 answer

1

Hello, my suspicion is that you are using the relative path being redirected to the same servlet, try to use the absolute path to this your jsp page, here's an example:

req.getRequestDispatcher("/WEB-INF/paginas/listaentidades.jsp").forward(req, resp);
    
05.07.2016 / 14:00