Doubt about servlets and jsp

0

The question is this, I have a jsp that consists of a simple form to edit the data of an object (clients), but for that to be possible, I must first check that the object to be edited exists on a base of data. So everything is perfect, just simply pass the data from a jsp to a servlet, and that returns me the data. The problem is that since from the beginning is the jsp with a get () to extract the data that is obtained after the action of the form, the first time it loads, it returns a null and I would like that to not happen. I have tried everything and I have not been able to achieve it.

JSP Code:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>

<%
    String numeroCredencial = (String) request.getAttribute("campoCredencialEncontrado");
    String nombre = (String) request.getAttribute("campoNombreEncontrado");
    String direccion = (String) request.getAttribute("campoDireccionEncontrado");
    String telefono = (String) request.getAttribute("campoTelefonoEncontrado");
%>

<body>
    <div>
        <form action = "actualizarcliente" method = "POST">
            Número de credencial<input type = "text" name = "campoCredencial"/>
            <input type = "submit" name = "botonEnviar" value = "Enviar"/>
        </form>
    </div>

    <div>
        <input type = "text" name = "campoCredencialEncontrado" value = "<%=numeroCredencial%>"/>
    </div>
    <div>
        <input type = "text" name = "campoNombreEncontrado" value = "<%=nombre%>"/>
    </div>
    <div>
        <input type = "text" name = "campoDireccionEncontrado" value = "<%=direccion%>"/>
    </div>
    <div>
        <input type = "text" name = "campoTelefonoEncontrado" value = "<%=telefono%>">
    </div>
</body>
</html>

Servlet Code:

protected void processRequest(HttpServletRequest request,  HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    /* TODO output your page here. You may use following sample code. */
    persistencia.PersistenciaBD nuevaPersistencia = new PersistenciaBD();

    String campoCredencial = request.getParameter("campoCredencial");
    Cliente buscarCliente = new Cliente(campoCredencial);

    if (nuevaPersistencia.obten(buscarCliente) != null) {
        Cliente clienteEncontrado = nuevaPersistencia.obten(buscarCliente);

        request.setAttribute("campoCredencialEncontrado", clienteEncontrado.getNumCredencial());
        request.setAttribute("campoNombreEncontrado", clienteEncontrado.getNombre());
        request.setAttribute("campoDireccionEncontrado", clienteEncontrado.getDireccion());
        request.setAttribute("campoTelefonoEncontrado", clienteEncontrado.getTelefono());

        request.getRequestDispatcher("actualizarcliente.jsp").forward(request, response);
    } else {
        request.setAttribute("campoCredencialEncontrado", "");
        request.setAttribute("campoNombreEncontrado", "");
        request.setAttribute("campoDireccionEncontrado", "");
        request.setAttribute("campoTelefonoEncontrado", "");

        request.getRequestDispatcher("actualizarcliente.jsp").forward(request, response);
    }

}
    
asked by David Antonio Hermosillo Solis 22.04.2018 в 22:50
source

2 answers

0

Your problem is this:

<form action = "actualizarcliente" method = "POST">
    Número de credencial<input type = "text" name = "campoCredencial"/>
    <input type = "submit" name = "botonEnviar" value = "Enviar"/>
</form>

Your form tag only includes the button, but for the data to travel to another page, each html control with significant information (such as inputs, textarea, selects, etc) must be within the form tag, and if not, the value of the control does not travel in the submit.

To correct it, place the rest of your code inside the form tag.

    
answered by 24.04.2018 в 18:16
0

To solve the problem you can do two things:

Option 1 : Initialize the variables without values and then validate by means of the sentence " IF ":

String numeroCredencial = ";
String nombre = "";
String direccion = "";
String telefono = "";

if(request.getAttribute("campoCredencialEncontrado")!=null){
   numeroCredencial  = (String) request.getAttribute("campoCredencialEncontrado");
}

if(request.getAttribute("campoNombreEncontrado")!=null){
   nombre = (String) request.getAttribute("campoNombreEncontrado");
}

if(request.getAttribute("campoDireccionEncontrado")!=null){
   direccion = (String) request.getAttribute("campoDireccionEncontrado");
}

if(request.getAttribute("campoTelefonoEncontrado")!=null){
  telefono = (String) request.getAttribute("campoTelefonoEncontrado");
}

Option 2 : Use the enhanced version of " IF ":

String numeroCredencial = (request.getAttribute("campoCredencialEncontrado")!=null)? ((String) request.getAttribute("campoCredencialEncontrado")) : "";

The latter seems a bit more complex but can be interpreted like this:

String variable = (expresión boleana)? "valor si verdadero" : "valor si falso";
    
answered by 08.01.2019 в 22:03