Servlet with JSP does not insert into the database

Asked

Viewed 588 times

3

I am doing my Interdisciplinary Project of the college and would be an e-commerce. Well, I was following the part of entering in the bank for a booklet of the course I did by Caelum, but I do not know if something is missing in the booklet, but when 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 entered normal. The error is right on the pages, but I don’t know where. Follows my codes.

Servlet additionUsuario

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

    }
}

cadastre.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 out the form formulario teste Then I give the REGISTER and the screen is like this pagina branca

If I fill out the wrong date like this data errada

It Accuses Conversion Error erro conversao

That is, it reaches the Servlet, but does not add in the Bank banco


I’m using Netbeans with Mysql and Glassfish. The server output when executing.jsp is

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
  • Look for the stacktrace in the logs (it’s something like that). If you are using an IDE (Eclipse, Netbeans, some other), it is usually shown in the console. Otherwise, look in the application server log folder (Tomcat, jboss, etc - I don’t know which you’re using..) In the message from stacktrace class, line and error will appear. Then come back here, click on [Edit] and place the error. Don’t forget to also tell which is the corresponding line in your code.

  • Try adding a bar before the action name in the action tag. By getting the following <form name="frmLogin" action="/additionUsuario" method="post">

  • Adding the bar gave error 404

  • No suitable driver found for jdbc:mysql:/localhost/Ecommerce - means that the Mysql driver is missing. See if you already have the file .jar corresponding, or download it in the mysql website. Then copy the jar into the respective folder (each server has its own folder). In the case of Eclipse, Netbeans, etc, it usually has some configuration screen to add the driver

2 answers

1

I tried to exchange the server (Glassfish 4.1.1) for Tomcat and the error of No suitable driver found persisted.

Looking for solutions I found this to register the driver with the DriverManager.Register before the 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);
        }
    }
}

Thank you all for your help

0

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.

  • I’ve changed in both jsp and html and remains the same. There’s no way to handle the exception?

  • What exception is he casting?

  • I don’t think any, but I needed some way out to find out what’s going on after I give Ubmit. Even in the Addresses bar the parameters are not showing

  • if the request is post the parameters do not appear in the address bar

Browser other questions tagged

You are not signed in. Login or sign up in order to post.