<web-app>
<servlet-mapping>
package jfc;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class Imagen extends HttpServlet {
public Imagen() {}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doGet(request,response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
OutputStream out = response.getOutputStream();
try {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
int i = 0;
for (i = 0; i<15;i++){
dataset.addValue((int)(Math.random()*100) , "Ventas", ""+i);
}
JFreeChart chart = ChartFactory.createBarChart("Tabla de Ventas", "Fecha", "Cantidad", dataset, PlotOrientation.VERTICAL, true, true, true);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, chart, 700, 400);
}catch (Exception e) {
System.err.println(e.toString());
} finally {
out.close();
}
}
}
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ejemplo JFreeChar JSP</title> </head> <body> <a href="servlet/Imagen">imagen</a> </body> </html>





