Não estou conseguindo implementar a solução usando o Class. Obtenho a seguinte exceção: java.lang.ClassNotFoundException:
jan 09, 2020 1:43:06 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [br.com.alura.gerenciador.servlet.UnicaEntradaServlet] in context with path [/gerenciador] threw exception [java.lang.ClassNotFoundException: LoginForm] with root cause
java.lang.ClassNotFoundException: LoginForm
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at br.com.alura.gerenciador.servlet.UnicaEntradaServlet.service(UnicaEntradaServlet.java:24)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Segue meu código:
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 {
// TODO Auto-generated method stub
String nome = null;
try {
String paramAcao = request.getParameter("acao");
Class classe = Class.forName(paramAcao);
Acao acao = (Acao) classe.newInstance();
nome = acao.executa(request, response);//Nome da acao
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
// TODO Auto-generated catch block
throw new ServletException(e);
}
String[] acao_endereco = nome.split(":");
if(acao_endereco[0].equals("foward")){
RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/view/" +
acao_endereco[1]);
rd.forward(request, response);
}else{
response.sendRedirect(acao_endereco[1]);
}
// if(paramAcao.equals("ListaEmpresas")) {
// ListaEmpresas acao = new ListaEmpresas();
// acao.executa(request, response);
// } else if(paramAcao.equals("RemoveEmpresa")) {
// RemoveEmpresa acao = new RemoveEmpresa();
// acao.executa(request, response);
// } else if(paramAcao.equals("MostraEmpresa")) {
// MostraEmpresa acao = new MostraEmpresa();
// acao.executa(request, response);
// } else if(paramAcao.equals("AlteraEmpresa")) {
// AlteraEmpresa acao = new AlteraEmpresa();
// acao.executa(request, response);
// } else if(paramAcao.equals("NovaEmpresa")) {
// NovaEmpresa acao = new NovaEmpresa();
// acao.executa(request, response);
// }
}
}