La primera tabla si que se me genera con el título, autor y precio. El fallo me da al querer recuperar los libros en los que seleccioné la opción comprar en una segunda tabla, y me da el siguiente error:
Código:
Estado HTTP 500 – Internal Server Error
Tipo Informe de Excepción
Descripción El servidor encontró un error interno que hizo que no pudiera rellenar este requerimiento.
excepción
java.lang.NullPointerException
sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
java.lang.Double.parseDouble(Double.java:538)
modelo.GestionVentas.service(GestionVentas.java:39)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
nota La traza completa de la causa de este error se encuentra en los archivos de registro del servidor.
Tipo Informe de Excepción
Descripción El servidor encontró un error interno que hizo que no pudiera rellenar este requerimiento.
excepción
java.lang.NullPointerException
sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
java.lang.Double.parseDouble(Double.java:538)
modelo.GestionVentas.service(GestionVentas.java:39)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
nota La traza completa de la causa de este error se encuentra en los archivos de registro del servidor.
GestionVentas.java
Código:
package modelo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import beans.Libro;
@WebServlet("/GestionVentas")
public class GestionVentas extends HttpServlet {
private static final long serialVersionUID = 1L;
public GestionVentas() {
super();
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Libro> lista = null;
HttpSession misesion = request.getSession();
lista= (ArrayList<Libro>)misesion.getAttribute("lista");
if (lista == null)
lista= new ArrayList<Libro>();
switch(request.getParameter("opcion")) {
case "comprar":
String titulo = request.getParameter("titulo");
String autor = request.getParameter("autor");
String precio = request.getParameter("precio");
double e = Double.parseDouble(precio);
Libro l = new Libro(titulo, autor, e);
lista.add(l);
misesion.setAttribute("lista", lista);
request.getRequestDispatcher("libros.jsp").forward(request, response);
break;
case "eliminar":
lista.remove(Integer.parseInt(request.getParameter("id")));
misesion.setAttribute("lista", lista);
request.getRequestDispatcher("libros.jsp").forward(request, response);
break;
}
}
}
---------------------------import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import beans.Libro;
@WebServlet("/GestionVentas")
public class GestionVentas extends HttpServlet {
private static final long serialVersionUID = 1L;
public GestionVentas() {
super();
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Libro> lista = null;
HttpSession misesion = request.getSession();
lista= (ArrayList<Libro>)misesion.getAttribute("lista");
if (lista == null)
lista= new ArrayList<Libro>();
switch(request.getParameter("opcion")) {
case "comprar":
String titulo = request.getParameter("titulo");
String autor = request.getParameter("autor");
String precio = request.getParameter("precio");
double e = Double.parseDouble(precio);
Libro l = new Libro(titulo, autor, e);
lista.add(l);
misesion.setAttribute("lista", lista);
request.getRequestDispatcher("libros.jsp").forward(request, response);
break;
case "eliminar":
lista.remove(Integer.parseInt(request.getParameter("id")));
misesion.setAttribute("lista", lista);
request.getRequestDispatcher("libros.jsp").forward(request, response);
break;
}
}
}
libros.jsp
Código:
<%@page import="beans.Libro"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML>
<html>
<head>
<title>libros</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h1>Listado de libros</h1>
<table border="1">
<tr><th></th><th>Titulo</th><th>Autor</th><th>Precio</th></tr>
<c:forEach items="${requestScope.libros}" var="libro" varStatus="i">
<tr>
<td><a href="GestionVentas?opcion=comprar&id=${i.index}">
<input type="submit" value="Comprar"/></a></td>
<td>${libro.titulo}</td>
<td>${libro.autor}</td>
<td>${libro.precio}</td>
</tr>
</c:forEach>
</table>
<br/><br/>
<br/><br/>
<table border=1 cellspacing=1 cellpadding=7 bordercolor="black">
<tr><th></th><th>Titulo</th><th>Autor</th><th>Precio</th></tr>
<c:forEach items="${sessionScope.lista}" var="libro2" varStatus="i">
<tr>
<td><a href="GestionVentas?opcion=eliminar&id=${i.index}">
<input type="submit" value="eliminar"/></a></td>
<td><c:out value="${libro2.titulo}"></c:out></td>
<td><c:out value="${libro2.autor}"></c:out></td>
<td><c:out value="${libro2.precio}"></c:out></td>
</tr>
</c:forEach>
</table>
<a href="Controller?op=doTemas">Otro tema</a>
<br/><br/>
</body>
</html>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML>
<html>
<head>
<title>libros</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h1>Listado de libros</h1>
<table border="1">
<tr><th></th><th>Titulo</th><th>Autor</th><th>Precio</th></tr>
<c:forEach items="${requestScope.libros}" var="libro" varStatus="i">
<tr>
<td><a href="GestionVentas?opcion=comprar&id=${i.index}">
<input type="submit" value="Comprar"/></a></td>
<td>${libro.titulo}</td>
<td>${libro.autor}</td>
<td>${libro.precio}</td>
</tr>
</c:forEach>
</table>
<br/><br/>
<br/><br/>
<table border=1 cellspacing=1 cellpadding=7 bordercolor="black">
<tr><th></th><th>Titulo</th><th>Autor</th><th>Precio</th></tr>
<c:forEach items="${sessionScope.lista}" var="libro2" varStatus="i">
<tr>
<td><a href="GestionVentas?opcion=eliminar&id=${i.index}">
<input type="submit" value="eliminar"/></a></td>
<td><c:out value="${libro2.titulo}"></c:out></td>
<td><c:out value="${libro2.autor}"></c:out></td>
<td><c:out value="${libro2.precio}"></c:out></td>
</tr>
</c:forEach>
</table>
<a href="Controller?op=doTemas">Otro tema</a>
<br/><br/>
</body>
</html>
Saludos