Problem displaying chart from Servlet to JSP

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
nimbus_cloud
Posts: 3
Joined: Thu Oct 19, 2006 2:47 pm

Problem displaying chart from Servlet to JSP

Post by nimbus_cloud » Thu Oct 19, 2006 2:51 pm

Hi guys! I have the following code listing. I was wondering why the graph wouldn't show up but just a silly image error icon... I'm testing this in IE.

Code: Select all

package controllers;

import java.io.FileOutputStream;
import java.io.File;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.UserBean;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartFactory;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.data.general.Dataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class GenericVerticalChartController extends HttpServlet
{
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{	
		HttpSession session = request.getSession();
		
		UserBean user = (UserBean)session.getAttribute("user");
		
		if ( user instanceof UserBean)
		{

		}
		else
		{
			user = new UserBean();
			user.setUserId( request.getParameter("userId") );
			
			session.setAttribute("user", user);
		}
		
		/*
		UserBean user = new UserBean();
		user.setUserId( request.getParameter("userId") );
		*/
		
		
	       double[][] data = new double[][] {
	               {1.0, 43.0, 35.0, 58.0, 54.0, 77.0, 71.0, 89.0},
	               {54.0, 75.0, 63.0, 83.0, 43.0, 46.0, 27.0, 13.0},
	               {41.0, 33.0, 22.0, 34.0, 62.0, 32.0, 42.0, 34.0}
	           };
	       
	        CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Series ", "Factor ", data);
		
	        JFreeChart chart = ChartFactory.createBarChart(
	                "Bar Chart Demo 2",         // chart title
	                "Category",                 // domain axis label
	                "Score (%)",                // range axis label
	                dataset,                    // data
	                PlotOrientation.VERTICAL, // orientation
	                true,                       // include legend
	                true,
	                false
	            );
	       
			
		
		response.setContentType("image/jpeg");
		
		/*
		 * Write JPEG Image to Servlet OutputStream 
		 */
		ChartUtilities.writeChartAsJPEG( response.getOutputStream(), chart, 765, 300);
		
		//response.getOutputStream().flush();
		//response.getOutputStream().close();
		
		return;
	}
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		doPost(request, response);
	}
}

I use it in my JSP like this...

<img src="${pageContext.request.contextPath}/generateverticalchart.do">


Any help would be appreciated. Thanks!
[/code]

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Post by angel » Fri Oct 20, 2006 7:22 am

Your source looks good, but I'm not sure if you call it corretly. Have you tried to call the GenericVerticalChartController directly in browser without the JSP stuff?

nimbus_cloud
Posts: 3
Joined: Thu Oct 19, 2006 2:47 pm

Post by nimbus_cloud » Fri Oct 20, 2006 11:24 am

Hi! Thanks for the reply. It appears to be a problem with mapping to web.xml file. Thanks!
:P

jfreeuser2006
Posts: 59
Joined: Mon Nov 20, 2006 1:00 pm

blank image

Post by jfreeuser2006 » Wed Jan 17, 2007 1:08 am

i was able to make a servlet that generates a chart png image.
when i ran it my local machine, it works fine but when i try to access the serlvet from another machine, it shows a blank image.

any clues?

jfreeuser2006
Posts: 59
Joined: Mon Nov 20, 2006 1:00 pm

Post by jfreeuser2006 » Mon Jan 22, 2007 6:17 am

got my servlet working. problem was due to some missing parameters.

Locked