Pessoal, estou migrando para java web e não estou conseguindo cadastrar os valores passados pelo formulário no banco de dados.
Pilha de erro:
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name: Apache Tomcat/9.0.45
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Mar 30 2021 10:29:04 UTC
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.45.0
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 10
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.0
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Program Files\Java\jdk-16.0.1
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 16.0.1+9-24
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: C:\bin\.metadata\.plugins\org.eclipse.wst.server.core\tmp2
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: C:\bin\apache-tomcat-9.0.45
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\bin\.metadata\.plugins\org.eclipse.wst.server.core\tmp2
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\bin\apache-tomcat-9.0.45
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\bin\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.base/java.util=ALL-UNNAMED
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.base/java.util.concurrent=ALL-UNNAMED
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
jul. 12, 2021 2:09:35 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -XX:+ShowCodeDetailsInExceptionMessages
jul. 12, 2021 2:09:35 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: Loaded Apache Tomcat Native library [1.2.27] using APR version [1.7.0].
jul. 12, 2021 2:09:35 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
jul. 12, 2021 2:09:35 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
jul. 12, 2021 2:09:35 PM org.apache.catalina.core.AprLifecycleListener initializeSSL
INFO: OpenSSL successfully initialized [OpenSSL 1.1.1k 25 Mar 2021]
jul. 12, 2021 2:09:36 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
jul. 12, 2021 2:09:36 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [1133] milliseconds
jul. 12, 2021 2:09:36 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
jul. 12, 2021 2:09:36 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.45]
jul. 12, 2021 2:09:39 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
jul. 12, 2021 2:09:40 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
jul. 12, 2021 2:09:40 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [3635] milliseconds
jul. 12, 2021 2:09:51 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [servlets.UsuarioSalvarServlet] in context with path [/Teste] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: javax.persistence.Persistence
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1364)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1187)
at repositorios.UsuarioRepositorio.cadastrarUsuario(UsuarioRepositorio.java:13)
at servlets.UsuarioSalvarServlet.doGet(UsuarioSalvarServlet.java:29)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
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:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
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:357)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:831)
Classe Usuário:
@Entity
public class Usuario {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(nullable = false)
private String nome;
@Column(nullable = false)
private int idade;
@Column(nullable = false)
private String sexo;
public Usuario() {
}
public Usuario(String nome, int idade, String sexo) {
super();
this.nome = nome;
this.idade = idade;
this.sexo = sexo;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public int getIdade() {
return idade;
}
public void setIdade(int idade) {
this.idade = idade;
}
public String getSexo() {
return sexo;
}
public void setSexo(String sexo) {
this.sexo = sexo;
}
}
Classe do repositório:
public class UsuarioRepositorio {
public void cadastrarUsuario(Usuario usuario) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("cadastro");
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
em.persist(usuario);
em.getTransaction().commit();
System.out.println("Salvo");
emf.close();
em.close();
} catch (RuntimeException e) {
em.getTransaction().rollback();
e.printStackTrace();
}
}
Classe da Servlet:
@WebServlet("/UsuarioSalvarServlet")
public class UsuarioSalvarServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String nome = req.getParameter("nome");
int idade = Integer.parseInt(req.getParameter("idade"));
String sexo = req.getParameter("sexo");
UsuarioRepositorio repositorio = new UsuarioRepositorio();
Usuario usuario = new Usuario(nome,idade,sexo);
try {
repositorio.cadastrarUsuario(usuario);
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.print("<html>");
out.print("<body>");
out.print("<h1>Usuário: " + nome + " cadastrado!<h1>");
out.print("</html>");
out.print("</body>");
}catch(RuntimeException e) {
e.printStackTrace();
}
}
Formulário Jsp:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>CADASTRO</h1>
<form action="<%=request.getContextPath()%>/UsuarioSalvarServlet"
method="get">
NOME <input type="text" name="nome"> <br>
<br> IDADE <input type="number" name="idade"> <br>
<br> SEXO<input type="text" name="sexo"> <br>
<br> <input type="submit" value="Enviar">
</form>
</body>
</html>