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);
}
}