http get method called automatically

Asked

Viewed 38 times

2

Good morning guys, I’m implementing the get a Servlet method here with a very simple logic.

If the user passes a url in the default: ".../entities/id", the editing form should be opened with the entity corresponding to the id entered after the bar. But if the url pattern is: ".../entities" the list of all entities will be loaded and the entity listing page opened.

Everything is already working, but with a problem. After running the last line it is facing the start of the get method, as if something is calling the get method, then it is in 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.

1 answer

1


Hello, my suspicion is that using the relative path this being redirected to the same Serrvlet, try to use the absolute path to that your jsp page, follow an example:

req.getRequestDispatcher("/WEB-INF/paginas/listaentidades.jsp").forward(req, resp);
  • Hello Leonardo Villela, thanks for the tip, I will test tonight and put here if it is solved.

  • Hello anything, if it helps do not forget to mark the question as answered and evaluate the answer, good studies :)

  • Thanks, Leonardo Villela, I decided here following your tip.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.