How to validate a form correctly?

2

I'm studying Java EE, but I'm having a hard time doing validations on data entry forms. My servlet CadastrarInstrutor collects the form fields and checks their contents.

Below is the code for my JSP page (I put only the content of the of my page, and the form of validation occurring in my servlet.

cadastra-instructor.jsp

<%
    String erros = "";
    String converter = "";
    if (request.getAttribute("erro") != null && request.getAttribute("erroConverter") != null) {
        erros = (String) request.getAttribute("erro");
        converter = (String) request.getAttribute("erroConverter");
    }
%>

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

    <p>Nome:
        <input type="text" name="nome"/>
        <span class="erro" style="color: red;">${erros}</span>
    </p>

    <p>Matrícula:
        <input type="text" name="matricula"/>
        <span class="erro" style="color: red;">${erros}</span>
    </p>

    <p>Conclusão de Gradução:
        <input type="text" name="graduacao"/>
        <span class="erro" style="color: red;">${erros}</span>
        <span class="erroConverter" style="color: red;">${erroConverter}</span>
    </p>

    <input type="submit" value="Enviar Dados"/>

</form>

RegisterInstructor.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    boolean isFormValido = true;
    try {
        String nome = request.getParameter("nome");
        String matricula = request.getParameter("matricula");
        String ano = request.getParameter("graduacao");
        if (nome.isEmpty() || matricula.isEmpty() || ano.isEmpty()) {
            request.setAttribute("erro", "Você não preencheu todos os campos!\");
            isFormValido = false;
        }
        Integer anoGraduacao = Integer.parseInt(ano);
    } catch (NullPointerException e) {
        request.setAttribute("erro", "Você não preencheu todos os campos!");
        isFormValido = false;
    } catch (NumberFormatException e) {
        request.setAttribute("erroConverter", "Apenas números são aceitos neste campo!");
        isFormValido = false;
    }

    if (isFormValido) {
        // Salvar no banco e então redirecionar para a listagem.
        response.sendRedirect("listagem-instrutor.jsp");
    } else {
        request.getRequestDispatcher("cadastro-instrutor.jsp").forward(request, response);
    }
}

By definition of the teacher (will be introduced in the future), for the time being, the use of JSTL is unavailable, so the use of scriptlets. There is also no focus on Javascript for form validation.

The question then is: How would you perform this validation, and where would you do it?

    
asked by anonymous 25.10.2015 / 16:58

1 answer

1

In pure servlet I would validate on the same screen, html5 already provide this feature, would also do as you are doing but I would create a class for each object to validate the attributes of it. I mean, I'd encapsulate the validations, understand?

Here's html5 running with validations: link

    
22.11.2015 / 15:45