Ver Mensaje Individual
  #1 (permalink)  
Antiguo 16/01/2017, 06:05
sentoki79
 
Fecha de Ingreso: octubre-2013
Mensajes: 79
Antigüedad: 10 años, 6 meses
Puntos: 1
Problema al selecciona filas a borrar con checkbox en JSP y servlet

Muy buenas a todos,


estoy desarrollando el tipico backend para mostrar filas, borrarlas, modificarlas, etc..
Y estoy teniendo problemas en el servlet que supuestamente recoje el ID de cada fila seleccionada para borrar por el checkbos seleccionado.
El error que me aparece es: java.lang.NullPointerException. Osea que esta recogiendo el valor de las variables en null.
El error creo que debe estar en el JSP que no he declarado bien el valor del ID ó en el action del form no redirijo correctamente al servlet. ö tal vez en el servlet no recojo adecuadamente los valores del checkbox.

Subo el codigo del JSP y del servlet. Gracias por las aportaciones :)


Codigo JSP:


<%@page import="Imagenes.*" %>
<%@page import="java.sql.*" %>
<%@page import="java.io.*" %>
<%@page import="javax.servlet.http.*" %>
<%@page import="javax.imageio.ImageIO" %>
<%@page import="javax.servlet.ServletOutputStream" %>
<%@page import="java.awt.image.BufferedImage" %>

<jsp:setProperty name="imagen" property="*" />

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<HTML ><HEAD><TITLE>Imagenes</TITLE></HEAD><BODY><HEADER><H1>Mostrar Imagenes</H1></HEADER>

<form action="http://localhost:8080/SubirImagenes/delete" method="Post" enctype='multipart/form-data'>
<TABLE><TR><TH >Nombre</TH><TH colspan='3'>FOTO</TH></TR>


<%
try {
String d = "com.mysql.jdbc.Driver";
Class.forName(d);
String sURL = "jdbc:mysql://localhost:3306/imagenes";
Connection Conexion;
Conexion = DriverManager.getConnection(sURL, "root", "oldViews17");


Statement misentencia;
ResultSet rs;
misentencia=Conexion.createStatement();
rs=misentencia.executeQuery("SELECT * FROM imagenes");
while (rs.next()) {
String nombre = rs.getString("Nombre");
int Id = rs.getInt("Id");
%>
<tr>
<td > <input type="checkbox" value='<%=Id %>' name='caja'></td>
<td > <%=Id %></td>
<td ><%=nombre %></td>

<td ><img src="http://localhost:8080/SubirImagenes/select?Id=<%=Id %>" width="190px" height="100px" align="right"></td></tr>


<%
}
}
catch (SQLException ex) {
}
catch(ClassNotFoundException ex)
{
}
%>

</table>
<input type="submit" value='Borrar'>
</form>

</body>
</html>



__________________________________

Codigo del servlet:



package Imagenes;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.*;


@WebServlet(urlPatterns = {"/delete"})
public class delete extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<HTML ><HEAD><TITLE>Borrado</TITLE></HEAD><BODY><HEADER><H1>Borrado</H1></HEADER>");

try {

String [] Array= request.getParameterValues("caja");


String d = "com.mysql.jdbc.Driver";
Class.forName(d);
String sURL = "jdbc:mysql://localhost:3306/imagenes";
Connection Conexion;
Conexion = DriverManager.getConnection(sURL, "root", "oldViews17");

PreparedStatement misentencia;

for (int j=0; j<Array.length-1; j++) {
String IdBorrar=Array[j];

misentencia = Conexion.prepareStatement("DELETE * FROM Imagenes WHERE Id='"+IdBorrar+"'");
ResultSet rs = misentencia.executeQuery();
}


} catch (SQLException ex) {

}
catch(ClassNotFoundException ex)
{

}
catch(NullPointerException ex)
{
out.println("<P>No envia variables desde el JSP</P>");
}

out.println("</body></html>");
}
}