3
respostas

Erro ao acessar http://localhost:8080/gerenciador/fazTudo?tarefa=BuscaEmpresa

Ocorre o erro: java.lang.IllegalStateException: Cannot forward after response has been committed br.com.alura.gerenciador.web.FazTudo.service(FazTudo.java:30) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) br.com.alura.gerenciador.web.FiltroDeAuditoria.doFilter(FiltroDeAuditoria.java:43)

O código está assim:

@Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.service(req, resp); String tarefa = req.getParameter("tarefa"); if(tarefa == null) throw new IllegalArgumentException("Voce esqueceu de passar a tarefa"); try { String nomeDaClasse = "br.com.alura.gerenciador.web." + tarefa; Class tipo = Class.forName(nomeDaClasse); Tarefa instancia = (Tarefa) tipo.newInstance(); String pagina = instancia.executa(req,resp); RequestDispatcher dispatcher = req.getRequestDispatcher(pagina); dispatcher.forward(req, resp); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { throw new ServletException(e); } }

3 respostas

Coloca em forma de código para facilitar a visualização dos membros do FÓRUM !!!

O erro que dá é esse:

java.lang.IllegalStateException: Cannot forward after response has been committed
    br.com.alura.gerenciador.web.FazTudo.service(FazTudo.java:30)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    br.com.alura.gerenciador.web.FiltroDeAuditoria.doFilter(FiltroDeAuditoria.java:43)

A linha 30 é a dispatcher.forward(req, resp);

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);

        String tarefa = req.getParameter("tarefa");

        if(tarefa == null)
            throw new IllegalArgumentException("Voce esqueceu de passar a tarefa");

        try {
            String nomeDaClasse = "br.com.alura.gerenciador.web." + tarefa;
            Class tipo = Class.forName(nomeDaClasse);
            Tarefa instancia = (Tarefa) tipo.newInstance();
            String pagina = instancia.executa(req,resp);
            RequestDispatcher dispatcher = req.getRequestDispatcher(pagina);
            dispatcher.forward(req, resp);
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            throw new ServletException(e);
        }

    }

O FiltroDeAuditoria está assim:

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;

        HttpSession session = req.getSession();
        Usuario usuarioLogado = (Usuario) session.getAttribute("usuarioLogado");

        String usuario = "<deslogado>";

        if (usuarioLogado != null) {
            usuario = usuarioLogado.getEmail();
        }

        System.out.println("Usuario " + usuario + " acessando a URI "
                + req.getRequestURI());

        chain.doFilter(request, response); // linha 43

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software