Servlet with JSP does not insert into bank
I'm doing my college interdisciplinary project and it would be an e-commerce. Well, I was following the part of inserting in the bank by an apostille of the course that I did for Caelum, but I do not know if something is missing in the Apostille, but at the time of sending the form, the page simply turns white and when I update the table in the Bank, nothing happened.
However, I ran a test class and it inserted normal. The error is even in the pages, but I do not know where. Follow my codes.
Servlet addressuser
@WebServlet(name = "adicionaUsuario", urlPatterns = {"/adicionaUsuario"})
public class adicionaUsuario extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
String nomeCompleto = request.getParameter("nomeCompleto");
String cpfcnpj = request.getParameter("cpfcnpj");
String dataEmTexto = request.getParameter("dataNascimento");
Calendar dataNascimento = null;
String endereco = request.getParameter("endereco");
String nmr = request.getParameter("nmr");
String cidade = request.getParameter("cidade");
String uf = request.getParameter("uf");
String cep = request.getParameter("cep");
String email = request.getParameter("email");
String senha = request.getParameter("senha");
try {
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dataEmTexto);
dataNascimento = Calendar.getInstance();
dataNascimento.setTime(date);
} catch (ParseException e){
out.println("Erro de conversao de data");
return;
}
Usuario usuario = new Usuario();
usuario.setNomeCompleto(nomeCompleto);
usuario.setCpfcnpj(cpfcnpj);
usuario.setDataNascimento(dataNascimento);
usuario.setEndereco(endereco);
usuario.setNrm(nmr);
usuario.setCidade(cidade);
usuario.setUf(uf);
usuario.setCep(cep);
usuario.setEmail(email);
usuario.setSenha(senha);
UsuarioDao dao = new UsuarioDao();
dao.adiciona(usuario);
out.println("<html>");
out.println("<body>");
out.println("Contato "+usuario.getNomeCompleto()+" Adicionado");
out.println("</body>");
out.println("</html>");
}
}
Registration.jsp
<%--
Document : login.jsp
Created on : 29/08/2018, 00:03:31
Author : Lucas Bighi
--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="
<c:url
value="include/vendor/bootstrap/css\bootstrap.css"/>
"
rel="stylesheet" type="text/css"/>
<link href="
<c:url value="include/css/\modern-business.css"/>
" r
el="stylesheet" type="text/css"/>
<meta name="viewport" content="width=device-width, initial-
scale=1, shrink-to-fit=no">
<title>Cadastro - Panela ADS</title>
</head>
<body>
<%-- Body--%>
<div class="cadastro">
<img src="<c:url value="img/logo-white.png"/>" width="200"/>
<h2>NOVO CADASTRO</h2>
<br>
<form name="frmLogin" action="adicionaUsuario" method="get">
Nome Completo: <input type="text" name="nomeCompleto" size="80" maxlength="30"/><br><br>
Tipo:      <input type="radio" name="tipoPessoa" value="pf"> Pessoa Física      
<input type="radio" name="tipoPessoa" value="pj"> Pessoa Jurídica<br><br>
CPF/CNPJ: <input type="text" name="cpfcnpj" size="13" maxlength="30"/><br><br>
Data de Nascimento: <input type="text" name="dataNascimento" size="10" maxlength="30"/><br><br>
Sexo:      <input type="radio" name="genero" value="pf"> Masculino      
<input type="radio" name="tipoPessoa" value="pj"> Feminino<br><br>
Endereço: <input type="text" name="endereco" size="80" maxlength="30"/>      Nº: <input type="text" name="nmr" size="4" maxlength="4"/><br><br>
Cidade: <input type="text" name="cidade" size="80" maxlength="30"/>      UF: <input type="text" name="uf" size="3" maxlength="2"/>      CEP: <input type="text" name="cep" size="10" maxlength="9"/><br><br>
Telefone 1: <input type="text" name="tel1" size="10"/>      Telefone 2: <input type="text" name="tel2" size="10"/>      Telefone 3: <input type="text" name="tel3" size="10"/><br><br>
E-mail: <input type="text" name="email" size="40" maxlength="20"/>      Confirme o e-mail: <input type="text" name="confEmail" size="40" maxlength="20"/><br><br>
Senha: <input type="password" name="senha" size="20" maxlength="16"/>      Confirme a senha: <input type="password" name="confSenha" size="20" maxlength="16"/><br><br>
<input type="checkbox" name="termos"> Eu li e concordo com os <a href="#">termos</a><br><br>
<button name="btnLogin">CADASTRAR</button>
</form>
</div>
</body>
</html>
cadastro.html (tentei em html pra ver se ia, mas também não foi)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="include/vendor/bootstrap/css\bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="include/css/\modern-business.css" rel="stylesheet" type="text/css"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Cadastro - Panela ADS</title>
</head>
<body>
<div class="cadastro">
<img src="img/logo-white.png" width="200"/>
<h2>NOVO CADASTRO</h2>
<br>
<form name="frmLogin" action="adicionaUsuario">
Nome Completo: <input type="text" name="nomeCompleto" size="80" maxlength="30"/><br><br>
Tipo:      <input type="radio" name="tipoPessoa" value="pf"> Pessoa Física      
<input type="radio" name="tipoPessoa" value="pj"> Pessoa Jurídica<br><br>
CPF/CNPJ: <input type="text" name="cpfcnpj" size="13" maxlength="30"/><br><br>
Data de Nascimento: <input type="text" name="dataNascimento" size="10" maxlength="30"/><br><br>
Sexo:      <input type="radio" name="genero" value="pf"> Masculino      
<input type="radio" name="tipoPessoa" value="pj"> Feminino<br><br>
Endereço: <input type="text" name="endereco" size="80" maxlength="30"/>      Nº: <input type="text" name="nmr" size="4" maxlength="4"/><br><br>
Cidade: <input type="text" name="cidade" size="80" maxlength="30"/>      UF: <input type="text" name="uf" size="3" maxlength="2"/>      CEP: <input type="text" name="cep" size="10" maxlength="9"/><br><br>
Telefone 1: <input type="text" name="tel1" size="10"/>      Telefone 2: <input type="text" name="tel2" size="10"/>      Telefone 3: <input type="text" name="tel3" size="10"/><br><br>
E-mail: <input type="text" name="email" size="40" maxlength="20"/>      Confirme o e-mail: <input type="text" name="confEmail" size="40" maxlength="20"/><br><br>
Senha: <input type="password" name="senha" size="20" maxlength="16"/>      Confirme a senha: <input type="password" name="confSenha" size="20" maxlength="16"/><br><br>
<input type="checkbox" name="termos"> Eu li e concordo com os <a href="#">termos</a><br><br>
<input type="submit" value="CADASTRAR"/>
</form>
</div>
</body>
</html>
Ai I fill out the form
I give the register and the screen is like this
If I fill in the wrong date like this
That is, it gets to the Servlet, but does not add to the Bank
I'm using Netbeans with MySQL and Glassfish. To exit the server when running the registration.jsp is
Grave: PWC6117: File "null" not found
After submitting the form the output is asism:
Advertência: StandardWrapperValve[adicionaUsuario]: Servlet.service() for servlet adicionaUsuario threw exception
java.lang.RuntimeException: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/ecommerce
at jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:21)
at dao.UsuarioDao.<init>(UsuarioDao.java:23)
at Business.adicionaUsuario.processRequest(adicionaUsuario.java:76)
at Business.adicionaUsuario.doPost(adicionaUsuario.java:114)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/ecommerce
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at jdbc.ConnectionFactory.getConnection(ConnectionFactory.java:19)
... 33 more
2 answers
I tried to exchange the server (Glassfish 4.1.1) for Tomcat and the error of in the suitable driver found persisted.
Looking for solutions I found this one to register the driver with DriverManager.Register
before return
in the connection class with the bank, getting this way:
public class ConnectionFactory {
public Connection getConnection() {
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
return DriverManager.getConnection("jdbc:mysql://localhost/ecommerce", "root", "root");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Thank you all for the help
Change the following snippet in your jsp file
Of
<form name="frmLogin" action="adicionaUsuario" method="get">
For
<form name="frmLogin" action="adicionaUsuario" method="post">
Regarding the date conversion, you have to validate it before submitting the form.