How to separate the HTML from the Java code of connection to the bank?

1

I have a database and an html code that finally managed to make a connection but the code is a bit messy because it mixes html with java and mysql ... Here's an example:

<%@page import="java.io.*,java.sql.*" %>    

<%
    Class.forName("com.mysql.jdbc.Driver");

    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/cadastro", "root","");
    String acao = request.getParameter("acao");
    if(acao == null){
        acao="listarPessoas";
    }

%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>cadastro</title>
</head>
<body>

    <form action="#" method="post">

        <%

            if(acao.equals("cadastro")){
                String nome = request.getParameter("nome");
                String idade = request.getParameter("idade");
                if(nome != null && idade != null){
                    String sql="INSERT INTO pessoa(nome,idade) VALUE(?,?)";
                    PreparedStatement stmt = conn.prepareStatement(sql);
                    stmt.setString(1, nome);
                    stmt.setString(2, idade);
                    stmt.execute();
                    out.println("pessoa" + " "+ nome + " " +idade );

                    acao="listarPessoas";

                    acao="novoCadastroPessoas";
                    out.println("<v> TODOS OS CAMPOS DEVER SER PREENCHIDOS</v>");
                }else{
                    acao="novoCadastroPessoas";
                    out.println("<v> TODOS OS CAMPOS DEVER SER PREENCHIDOS</v>");
                }
            }







            if(acao.equals("novoCadastro")){


        %>

        <label for="nome">nome:</label>
        <input type = "text " name="nome">
        <label for="idade">idade:</label>
        <input type="date" id="idade">
        <button type="submite" name="acao" value="listarPessoas">voltar</button>
        <button type="submite" name="acao" value="cadastro">salvar</button>

        <%
            }else if(acao.equals("listarPessoas")){
        %>

        <button type="submit" name="acao" value="novoCadastro">Novo cadastro</button>
        <fieldset>
            <table>
                <thead>
                    <tr>
                        <th>Codigo</th>
                        <th>Nome</th>
                        <th>Data de nascimento</th>
                    </tr>
                </thead>
            </table>
                <tr>
                    <tb></tb>
                    <tb></tb>
                    <tb></tb>
                </tr>   

        <fieldset>
        <%
            }
        %>
    </form>


</body>
</html>

How can I make html and java do not mix, creating classes for functions like connect to the database or to extract information? And where to put the functions in the code?

    
asked by anonymous 22.12.2018 / 19:09

1 answer

1

If you want to make a nice break I recommend studying about the DAO (Data Access Object)

About DAO

Later on MVC (Model View Controller) , for this I recommend to study a little more about:

HTTP: To understand more about request and response and how the main web protocol is used.
SERVLET: This is the basis of most (if not all) Java MVC frameworks, understanding how it works will be critical.

About MVC

Note: The two standards mentioned allow you to make a nearly complete separation of the backend and the frontend, you have more good options like Repository but DAO and MVC believe they are the gateway to a good web project divided.

    
23.12.2018 / 22:00