Entering data automatically when loading the JSP page

Asked

Viewed 372 times

0

I am developing a web application using jsp, in which I register a mototrista but soon when loading the jsp page the application already tries to register alone the driver data, thus giving the error

inserir a descrição da imagem aqui

<%@page import="java.util.List"%>
<%@page import="Classes.MotoristasDAO"%>
<%@page import="Classes.Motoristas"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.util.logging.Logger"%>
<%@page import="java.util.logging.Level"%>
<%@page import="Classes.Conecta"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script src="js/jquery-1.12.0.min.js" type="text/javascript"></script>
    <script src="js/jquery.maskedinput.js" type="text/javascript"></script>
      <script src="js/jquery.maskedinput.min.js" type="text/javascript"></script>

      <script src="bootstrap/bootstrap.min.js" type="text/javascript">
      </script>
      <script src="bootstrap/bootstrap.js" type="text/javascript"></script>
      <link rel="stylesheet" href="bootstrap/bootstrap.css">
      <link rel="stylesheet" href="css/css.css" type="text/css">
    <title>JSP Page</title>

    </head>
    <body>
    <%

    Motoristas mot= new Motoristas();
    mot.setMo_nome(request.getParameter("nome_passageiro"));
    mot.setMo_cpf(request.getParameter("cpf"));
    mot.setMo_modelo(request.getParameter("modelo"));
    mot.setMo_status(request.getParameter("status"));
    mot.setMo_sexo(request.getParameter("sexo"));
    mot.setMo_data(request.getParameter("dt_nascimento"));

        MotoristasDAO dao= new  MotoristasDAO();
        Boolean ret=dao.cadastrarMotorista(mot);
        String msg1,msg2;
        if(ret==true){

        msg1="Cadastro Realizado";

        }
        else{
        msg1="Cadastro Realizado";
        }

    List<Motoristas> lista;
    lista=dao.mostrarMotoristas();


    if(request.getParameter("nome_passageiro")==null){
    msg2="PArametro não passado";

    }
    else{
    Motoristas mot2= new Motoristas();
    mot2.setMo_nome(request.getParameter("nome_passageiro"));
    Boolean excluir=dao.excluirMotorista(mot2);

    if(excluir==true){
     msg2="Excluido com sucesso";
     }
      else{
     msg2="Erro ao excluir";
     }
     }

        %>


    <div class="conteudo2">
    <img src="imagens/imagens.jpg" alt="motorista" id="motorista">
    </div>
     <nav id="menu">

        <div class="conteudo"> <a href="index.jsp">Controle Motoristas</a>
     </div>
        <div class="conteudo"> <a href="passageiros.jsp">Controle 
     Passageiros</a></div>
        <div class="conteudo"><a href="#">Controle Corridas</a></li></div>
       </nav>

       <div class="row">

           <div class="col-6 col-md-4"></div>
                 <div class="col-6 col-md-4">
    <h1>Cadastro de Motoristas</h1>
               </div>
            <div class="col-6 col-md-4"></div>

   </div>
        <form method="get">
       <div class="row">
 <div class="col-6 col-md-4">

  Nome do Motorista:<input type="text" id="nome_passageiro2" name="nome_passageiro"><br/><br/>
 </div>

 <div class="col-6 col-md-4">
    Data de Nascimento:<input type="text" id="dt_nascimento" name="dt_nascimento"><br/><br/>
 </div>

  <div class="col-6 col-md-4">
    CPF:<input type="text" id="cpf" name="cpf" ><br/><br/>
  </div>
    </div>

    <div class="row">
        <div class="col-6 col-md-4">
    Modelo de Carro:<input type="text" id="modelo" name="modelo"><br/><br/>
        </div>

            <div class="col-6 col-md-4">
    Status:  <label for="s1">Ativo</label>
        <input type="radio" id="ativo" name="status" value="ativo"/>

        <label for="s2">Inativo</label>
        <input type="radio" id="inativo" name="status" value="inativo"/><br/><br/>
            </div>

             <div class="col-6 col-md-4">
    Sexo:  <label for="s4">Masculino</label>
        <input type="radio" id="masc" name="sexo" value="masculino"/>

        <label for="s6">Feminino</label>
        <input type="radio" id="fem" name="sexo" value="feminino"/><br/><br/>

        </div>
    </div>

            <%=msg1%>.<br>
             <%=msg2%>.<br>
        <button type="submit" onclick="validar()" >Cadastrar</button> 
 <button type="button" onclick="validarConsulta()" >Alterar</button> 
 <button type="button" onclick="validarConsulta()" >Consultar</button><br/><br/>

  <table border="1">
      <tr>
          <th>Nome</th> 
           <th>Data</th>
           <th>CPF</th>
           <th>Modelo</th>
           <th>Status</th>
           <th>Sexo</th>
           <th>Ação</th>

      </tr>
      <%
          for(Motoristas registro:lista){



          %>

          <tr>
              <td><%=registro.getMo_nome()%></td>
          <td><%=registro.getMo_data() %></td>
          <td><%=registro.getMo_cpf() %></td>
           <td><%=registro.getMo_modelo() %></td>
            <td><%=registro.getMo_status() %></td>
             <td><%=registro.getMo_sexo() %></td>
             <td><a href="#?login=<%=registro.getMo_nome()%>"


                    >Excluir</a></td>
      </tr>
      <%
          }
          %>
  </table> 

    </form>
    <footer>

        Felipe Patrocinio [email protected]
    </footer>

</body>

1 answer

0


Your JSP should only contain the form code, leaving the CRUD operations to, for example, a Servlet.

The error in question occurs because at the beginning of the page you load a scriplet that contains the code

Boolean ret=dao.cadastrarMotorista(mot);

Another detail is that the swing windows are not rendered in the browser, but on the machine that will host the application server.

  • then in case the scriplet is executed when loading the page? which contains the method of registering driver, thus trying to register it every time the page is loaded ?

  • Yes, exactly.

Browser other questions tagged

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