Servlet with JSP does not insert into the database

3

I'm doing my Interdisciplinary College Project and it would be an e-commerce. Well, I was following the part of inserting in the bank for a course book I made for Caelum, but I do not know if something is missing in the book, but at the time of sending the form, the page simply goes white and when I update the table in the bank, nothing happened.

However, I ran a test class and it entered normal. The error is actually on the pages, but I do not know where. Follow my codes.

Servlet adds User

@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>");

    }
}

cadastro.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:&nbsp &nbsp &nbsp  <input type="radio" name="tipoPessoa" value="pf"> Pessoa Física &nbsp &nbsp &nbsp
            <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:&nbsp &nbsp &nbsp  <input type="radio" name="genero" value="pf"> Masculino &nbsp &nbsp &nbsp
            <input type="radio" name="tipoPessoa" value="pj"> Feminino<br><br>
            Endereço:    <input type="text" name="endereco" size="80" maxlength="30"/>&nbsp &nbsp &nbsp  Nº: <input type="text" name="nmr" size="4" maxlength="4"/><br><br>
            Cidade:    <input type="text" name="cidade" size="80" maxlength="30"/>&nbsp &nbsp &nbsp  UF: <input type="text" name="uf" size="3" maxlength="2"/>&nbsp &nbsp &nbsp  CEP: <input type="text" name="cep" size="10" maxlength="9"/><br><br>
            Telefone 1:    <input type="text" name="tel1" size="10"/>&nbsp &nbsp &nbsp  Telefone 2: <input type="text" name="tel2" size="10"/>&nbsp &nbsp &nbsp  Telefone 3: <input type="text" name="tel3" size="10"/><br><br>
            E-mail:    <input type="text" name="email" size="40" maxlength="20"/>&nbsp &nbsp &nbsp  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"/>&nbsp &nbsp &nbsp  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:&nbsp &nbsp &nbsp  <input type="radio" name="tipoPessoa" value="pf"> Pessoa Física &nbsp &nbsp &nbsp
            <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:&nbsp &nbsp &nbsp  <input type="radio" name="genero" value="pf"> Masculino &nbsp &nbsp &nbsp
            <input type="radio" name="tipoPessoa" value="pj"> Feminino<br><br>
            Endereço:    <input type="text" name="endereco" size="80" maxlength="30"/>&nbsp &nbsp &nbsp  Nº: <input type="text" name="nmr" size="4" maxlength="4"/><br><br>
            Cidade:    <input type="text" name="cidade" size="80" maxlength="30"/>&nbsp &nbsp &nbsp  UF: <input type="text" name="uf" size="3" maxlength="2"/>&nbsp &nbsp &nbsp  CEP: <input type="text" name="cep" size="10" maxlength="9"/><br><br>
            Telefone 1:    <input type="text" name="tel1" size="10"/>&nbsp &nbsp &nbsp  Telefone 2: <input type="text" name="tel2" size="10"/>&nbsp &nbsp &nbsp  Telefone 3: <input type="text" name="tel3" size="10"/><br><br>
            E-mail:    <input type="text" name="email" size="40" maxlength="20"/>&nbsp &nbsp &nbsp  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"/>&nbsp &nbsp &nbsp  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>

Then I fill in the form ThenIgivetheREGISTERandthescreenstayslikethis

IfIfillinthewrongdatelikethis

heaccusesConversionError

Thatis,itreachesServlet,butdoesnotaddtotheBank

I'musingNetbeanswithMySQLandGlassfish.Theserveroutputwhenexecutingtheregister.jspis

Grave:PWC6117:File"null" not found

After giving the submit in 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
    
asked by anonymous 08.11.2018 / 04:54

2 answers

1

I tried to change the server (Glassfish 4.1.1) through Tomcat and the No suitable driver found error persisted.

Looking for solutions I found this to register the driver with DriverManager.Register before return in the connection class with the bank, thus:

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);
        }
    }
}

Thanks to everyone for the help

    
09.11.2018 / 03:15
0

Change the following snippet in your jsp file

de

<form name="frmLogin" action="adicionaUsuario" method="get">

for

<form name="frmLogin" action="adicionaUsuario" method="post">

Regarding date conversion, you have to validate it before submitting the form.

    
08.11.2018 / 10:53