Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Erro ao executar

GRAVE: Servlet.service() for servlet [br.com.alura.gerenciador.servlet.UnicaEntradaServlet] in context with path [/gerenciador] threw exception [java.lang.ClassNotFoundException: br.com.alura.gerenciador.acaonull] with root cause java.lang.ClassNotFoundException: br.com.alura.gerenciador.acaonull at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1412) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1220) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:375) at br.com.alura.gerenciador.servlet.UnicaEntradaServlet.service(UnicaEntradaServlet.java:30) at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1787) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:833) ` package br.com.alura.gerenciador.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import br.com.alura.gerenciador.acao.Acao;

@WebServlet("/entrada") public class UnicaEntradaServlet extends HttpServlet { private static final long serialVersionUID = 1L;

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String paramAcao = request.getParameter("Acao");
    String nomeDaClasse = "br.com.alura.gerenciador.acao" + paramAcao;



    String nome;
    try {
        Class classe = Class.forName(nomeDaClasse);//carrega a classe com o nome 
        Acao acao = (Acao) classe.newInstance(); 
        nome = acao.executa(request, response);
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new ServletException(e);
    }

    String[] tipoEEndereco = nome.split(":");

    if(tipoEEndereco[0].equals("forward")) {        
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/view/" + tipoEEndereco[1]);
        rd.forward(request, response);
    } else {
        response.sendRedirect(tipoEEndereco[1]);
    }

}

}

`

package br.com.alura.gerenciador.acao;

import java.io.IOException; import java.util.List;

import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import br.com.alura.gerenciador.modelo.Banco; import br.com.alura.gerenciador.modelo.Empresa;

public class ListaEmpresas implements Acao{

public String executa(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    System.out.println("listando empresas");

    Banco banco = new Banco();
    List<Empresa> lista = banco.getEmpresas();

    request.setAttribute("empresas", lista);

    return "forward:listaEmpresas.jsp";


}

}

`
2 respostas

Tenta colocar . depois de acao deve resolver o problema

String nomeDaClasse = "br.com.alura.gerenciador.acao." + paramAcao;

solução!

voce deve ter escrito o valor do parametro errado no codigo fonte: String paramAcao = request.getParameter("Acao");

Tente trocar por: String paramAcao = request.getParameter("acao");

O "A" está em maiusculo.

Além disso, como o Eder citou, falta o . depois da acao.