Usando Java 17 e Tomcat 9 Servlet:
package br.com.arthur.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.arthur.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 packageAction = "br.com.alura.gerenciador.acao.";
String nomeDaClasse = "br.com.alura.gerenciador.acao." + paramAcao;
String nome;
try {
Class classe = Class.forName(nomeDaClasse);
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]);
}
}
}
Interface:
package br.com.arthur.gerenciador.acao;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Acao {
String executa(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
Ação ListaEmpresas
package br.com.arthur.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.arthur.gerenciador.modelo.Banco;
import br.com.arthur.gerenciador.modelo.Empresa;
public class ListaEmpresas implements Acao{
public String executa(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Banco banco = new Banco();
List<Empresa> lista = banco.getLista();
//chamar jsp
request.setAttribute("empresas", lista);
System.out.println("Listando empresa");
return "forward:listaDasEmpresas.jsp";
}
}
Erro: dez. 28, 2021 7:57:36 PM org.apache.catalina.core.StandardWrapperValve invoke GRAVE: Servlet.service() for servlet [br.com.arthur.gerenciador.servlet.UnicaEntradaServlet] in context with path [/gerenciador] threw exception [java.lang.ClassNotFoundException: br.com.alura.gerenciador.acao.NovaEmpresaForm] with root cause java.lang.ClassNotFoundException: br.com.alura.gerenciador.acao.NovaEmpresaForm at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1407) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1215)