JSP Servlet suma forEach

beatlebum
16 de Junio del 2009
Hola, espero me puedan ayudar.

Estoy haciendo un proyecto en NetBeans usando JSP y Servlets. Y bueno mi problema consiste en que al hacer una consulta me aparecen los registros, los cuales tienen unos valores que son los precios y lo que quiero es que me de un total de la suma de los precios de los registros.

Mi JSP es este:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<HTML>
<HEAD>

<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="style.css" type="text/css">

<TITLE>Peliculas Seleccionadas</TITLE>
</HEAD>
<BODY>
<center>
<H2>Peliculas Seleccionadas</H2>
<TABLE cellspacing="10" cellpading="3">
<tr>
<th>Clave</th>
<th>Titulo</th>
<th>Precio</th>
</tr>
<c:forEach var="objeto" items="${objetos}" >
<tr>
<td><b>${objeto.clave}</b></td>
<td><b>${objeto.titulo}</b></td>
<td><h2>$ ${objeto.precio}</h2></td>
</tr>
</c:forEach>
</TABLE>
</center>
</BODY>
</HTML>

Donde ${objeto.precio} son los valores que quiero que se sumen.

Este es mi servlet:

package servlets;

import control.CtrlTotal;
import entity.Pelicula;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;

public class SPTotal extends HttpServlet {
@Resource(name="jdbc/peliculas")
private DataSource ds;
private CtrlTotal ctrlTotal;

@Override
public void init() throws ServletException{
ctrlTotal = new CtrlTotal(ds);
}

@Override
protected void doGet(HttpServletRequest solicitud, HttpServletResponse respuesta)
throws ServletException, IOException {
try{
Pelicula[] objetos = ctrlTotal.consulta();
solicitud.setAttribute("objetos", objetos);
RequestDispatcher despachador = solicitud.getRequestDispatcher("/JSPTotal.jsp");
despachador.forward(solicitud, respuesta);
}catch(SQLException e){
throw new ServletException(e);
}
}
}

Y este mi controlador que tambien es una clase en Java.

package control;

import entity.Pelicula;
import java.sql.SQLException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class CtrlTotal {
DataSource ds;

public CtrlTotal(DataSource ds) {
this.ds = ds;
}

public Pelicula[] consulta() throws SQLException{
Connection c = null;
Statement s = null;
ResultSet rs = null;
try{
c = ds.getConnection();
s = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = s.executeQuery("SELECT * FROM seleccion");
rs.last();
int total = rs.getRow();
Pelicula[] objetos = new Pelicula[total];
rs.beforeFirst();
for(int i=0; rs.next() && i<objetos.length; i++){
Pelicula objeto = new Pelicula();
objeto.setClave(rs.getString("con_clave"));
objeto.setTitulo(rs.getString("con_titulo"));
objeto.setPrecio(rs.getInt("con_precio"));
objetos[i] = objeto;
}
return objetos;
}finally{
if(c != null){
try{
c.close();
}catch(SQLException e){}
}
if(s != null){
try{
s.close();
}catch(SQLException e){}
}
if(rs != null){
try{
rs.close();
}catch(SQLException e){}
}
}
}
}

agradeceria su ayuda. Saludos.