• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to generate a chart by using jchart & jsp

 
Benjamin Ho
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everybody,
I met problems on generating chart by using jchart and jsp. the environment is with jCharts-0.7.0, j2sdk1.4.2_01, jbuilder development platform. there are the errors and code. The following are the errors:
ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: getOutputStream() has already been called for this response
ERROR [Engine] ----- Root Cause -----
java.lang.IllegalStateException: getOutputStream() has already been called for this response
The following is the code:
<%@ page import="java.awt.*,org.jCharts.*,org.jCharts.chartData.*,org.jCharts.properties.*,org.jCharts.types.ChartType,org.jCharts.axisChart.*,org.jCharts.test.TestDataGenerator,org.jCharts.encoders.*,org.jCharts.properties.util.ChartFont"%>
<%
try
{
String[] xAxisLabels= { "2001", "2002", "2003", "2004", "2005", "2006", "2007" };
String xAxisTitle= "Models";
String yAxisTitle= "Unit Sold";
String title= "Product Sold";
DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title );
double[][] data= new double[][]{ { 300, 10, 200, 70, 140, 20, 10 } };
String[] legendLabels= { "quantity" };
Paint[] paints= new Paint[]{ Color.cyan.brighter() };
BarChartProperties barChartProperties= new BarChartProperties();
AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, barChartProperties );
dataSeries.addIAxisPlotDataSet( axisChartDataSet );
ChartProperties chartProperties= new ChartProperties();
//---to make this plot horizontally, pass true to the AxisProperties Constructor
//AxisProperties axisProperties= new AxisProperties( true );
AxisProperties axisProperties= new AxisProperties();
LegendProperties legendProperties= new LegendProperties();
//AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, 550, 360 );
Chart chart=(Chart) new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, 550, 360 );
HttpServletReponse response=new HttpServletResponse();
ServletEncoderHelper.encodeJPEG13(chart, 1.0f, response);
//JPEGEncoder13.encode( chart, 1.0f, response );

} catch(Exception e)
{
System.out.println("this is the exception");
System.out.println(e);
}
%>
<html>
.....
</html>
Based on the above code, how to solve it if I need use jsp to program.
Urgent!! Please tell what happens and how to solve it!!
best regards,
benjamin
[ March 09, 2004: Message edited by: Benjamin Ho ]
[ March 09, 2004: Message edited by: Benjamin Ho ]
[ March 09, 2004: Message edited by: Bear Bibeault ]
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure whether this is a problem. But should your code have this:

JSP defines some implicit objects, like request, response, exception, page, session, etc.
I think you do not need to declare one, as you have it "intentionally". In fact, for each JSP, only 1 response should be hold.
But now, you are trying to define 1 more, and thus, maybe this is the cause of the IllegalStateException.
Could you try removing it and try the code again?
Nick.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not try to mix binary and text data in the same JSP/servlet.
What you should rather do is have the JSP contain an <img ... > tag which links to a servlet in which you generate the graph.
You can use the dynamic nature of JSP to create a dynamic URL containing parameters for the servlet (and set data in the session if needed).
 
Benjamin Ho
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everybody,
hey, Nicholas Cheung and Jeroen Wenting, thank you for your suggestion. Since I am a beginner and dont know how to code servlet, i want to program the chart on JSP. Also the program is quickly handed, i have no time to study servlet.
Please tell me how to generate chart on jsp. Also I have coded the following but there are errors occured.
HttpServletResponse servletResponse=new HttpServletResponse();
ServletEncoderHelper.encodeJPEG13(chart, 1.0f, servletResponse);

The errors are 1. interface javax.servlet.http.HttpServletResponse is abstract; cannot be instantiated at line 62, constructor HttpServletResponse() not found in interface javax.servlet.http.HttpServletResponse at line 62.

How do I fix the bug?? Please tell me!!
best regards,
benjamin
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Benjamin Ho:
HttpServletResponse servletResponse=new HttpServletResponse();
ServletEncoderHelper.encodeJPEG13(chart, 1.0f, servletResponse);
The errors are 1. interface javax.servlet.http.HttpServletResponse is abstract; cannot be instantiated at line 62, constructor HttpServletResponse() not found in interface javax.servlet.http.HttpServletResponse at line 62.

The error is simple... Since HttpServletResponse is an interface, u cannot instantiate it... But you can assign from another HttpServletResponse... If you are dealing with that code in the doPost() method, you can use the HttpServletResponse object from the paramater of the method and assign it to the HttpServletResponse u want... Hope it is clear... I think the following code snippet might help you as an example...

public void doPost(HttpServletRequest request, HttpServletResponse response)
HttpServletResponse servletResponse = response;
ServletEncoderHelper.encodeJPEG13(chart, 1.0f, servletResponse);
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Benjamin,
As I said before, response is a HttpServletResponse that PRE-DEFINED in JSP. Thus, you do not have to new an instance of it, or doing any initialization.
You can use it directly without doing any declarations.
Nick.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, forget to say, thus, you should change your code FROM:

TO:

Hope this help.
Nick.
 
Benjamin Ho
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everybody,
Ko Ko Naing and Nicholas Cheung, thank you for your help.
Nicholas Cheung, I have tested and errors occured after coding like the following.
// [DELETE THIS]HttpServletResponse servletResponse=new HttpServletResponse();ServletEncoderHelper.encodeJPEG13(chart, 1.0f, response);
The error as:
ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: getOutputStream() has already been called for this response
ERROR [Engine] ----- Root Cause -----
java.lang.IllegalStateException: getOutputStream() has already been called for this response
Ko Ko Naing, I think the doPost() method that would make me in bored state. I need time to think how to do the method. Anyway, Thank you for your suggestion.
best regards,
benjamin
[ March 09, 2004: Message edited by: Benjamin Ho ]
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Benjamin Ho:
Ko Ko Naing, I think the doPost() method that would make me in bored state. I need time to think how to do the method. Anyway, Thank you for your suggestion.

Benjamin, as long as you are developing JSPs- and servlet-related stuff, you cannot be bored of doPost() method... It's an essential method for servlets and if you ignore the importance of this method, then u're done...
It's easy to fetch the concept of servlet... If you have desire to learn, it'll take no time... Within two or three days, you will get it... Just my suggestion...
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the method:

perform?
It seems that you should not pass a response to it, because the response, in fact, is your JSP.
If, the method does sth with the response and then send the response to back to the client, then there will be an IllegalStateException, cos your current JSP's response does not complete (as you still have html codes after the scriptlet).
You should check what the API does.
Nick.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin, what you are trying to do is NOT possible using just a JSP.
You need that servlet to produce the image data, as you cannot mix text and binary data in your outputstream (and JSP produces text data).
JSPs are compiled into servlets at first call (or before, depending on deployment mechanism of your server), and the output initialised to text format by default.
You MIGHT be able to do as I suggested but have the image produced by a JSP instead, you'd then have to force the contentType to the correct one for JPEG ("image:jpeg" I think it was, or something similar) which you'd have to do in the servlet also).
But creating it as a servlet is easy and painless. All you need is bare servlet code and implement the doGet() method (or service()).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!