I created a JSP that generates a chart and sends it directly to the browser.
I tested this code in Forte 3.0. I put it in a new directory called jsp in the JFreeChart demo directory. Forte automatically creates a web module (WEB-INF) for you. Make sure you have the JFreeChart jar files mounted. Then just Compile & Execute (Forte automatically configures tomcat and will automatically bring up your browser (except Netscape 6).
I am going to try to do another one that will directly output SVG instead of JPEG.
Marcus
<%@page contentType="text/html"%>
<%@page import="com.jrefinery.data.DefaultPieDataset" %>
<%@page import="com.jrefinery.chart.ChartFactory" %>
<%@page import="com.jrefinery.chart.JFreeChart" %>
<%@page import="com.jrefinery.chart.JFreeChartFrame" %>
<%@page import="java.io.IOException" %>
<%@page import="java.io.OutputStream" %>
<%@page import="com.jrefinery.chart.*" %>
<%@page import="java.awt.Color" %>
<%@page import="java.awt.GradientPaint" %>
<html>
<head><title>JPEG JFreeChart JSP (Java Server Page) Example</title></head>
<body>
<%-- The Chart below is sent directly to the browser as a JPEG Output Stream. --%>
<% createWebChart(response); %>
<%!
/**
* createWebChart
* @author Marcus Wilhoit, NiSUSTECH, 05.14.2002
* @param httpServletResponse HttpServletResponse
* @throws IOException catch standard IOException
*/
public void createWebChart(HttpServletResponse httpServletResponse) throws IOException
{
// create a dataset...
DefaultPieDataset data = new DefaultPieDataset();
data.setValue("Category 1", new Double(43.2));
data.setValue("Category 2", new Double(27.9));
data.setValue("Category 3", new Double(79.5));
data.setValue("Category 4", new Double(66.5));
// create a chart...
JFreeChart chart = ChartFactory.createPieChart("Sample Pie Chart", data, true);
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.orange));
PiePlot plot = (PiePlot)chart.getPlot();
plot.setCircular(false);
// make section 1 explode by 100%...
plot.setRadiusPercent(0.60);
plot.setExplodePercent(1, 1.00);
httpServletResponse.setContentType("image/jpeg");
OutputStream out = httpServletResponse.getOutputStream();
int width = 670; //400
int height = 356; //300
ChartUtilities.writeChartAsJPEG(out, chart, width, height);
out.close();
}
%>
</body>
</html>
JSP (Java Server Page) JPEG JFreeChart Example
Re: JSP (Java Server Page) JPEG JFreeChart Example
Hrmm, doesn't work for me on Jrun 3.0. I get the following (partial) trace:
null
java.lang.IllegalStateException
at allaire.jrun.servlet.JRunResponse.getOutputStream(../servlet/JRunResponse.java:294)
A useful error message indeed. Investigating further, the error comes at the line
OutputStream out = httpServletResponse.getOutputStream();
I vaguely recall some problem with getting the OutputStream multiple times in a jsp. Since html has already been written to the output stream, the IllegalStateException occurs.
Not sure why it works on tomcat though.
null
java.lang.IllegalStateException
at allaire.jrun.servlet.JRunResponse.getOutputStream(../servlet/JRunResponse.java:294)
A useful error message indeed. Investigating further, the error comes at the line
OutputStream out = httpServletResponse.getOutputStream();
I vaguely recall some problem with getting the OutputStream multiple times in a jsp. Since html has already been written to the output stream, the IllegalStateException occurs.
Not sure why it works on tomcat though.
Re: JSP (Java Server Page) JPEG JFreeChart Example
I works just fine for me in JRun 4.0 DeveloperEdition. I put the jsp file in the default-war directory that is inside the default-ear directory. Then I make sure the JFreeChart jar files are in the JRun 4.0 classpath. I do not get a java.lang.IllegalStateException with JRUn 4.0.
Marcus
Marcus
Re: JSP (Java Server Page) JPEG JFreeChart Example
Allaire (along with a number of other vendors) changed the point at which JSP pages got the OutputStream with version 3.x of JRun. This prevented delivering binary content using JSP pages and broke a lot of my own code in the process! No-one was particularly happy about it and the JSP specification was tightened up with version 1.2 so that delivery binary content with JSP pages is now a condition of meeting the spec.
If you want to get the example to work under JRun 3.x you will need to rewrite it as a servlet.
Richard...
If you want to get the example to work under JRun 3.x you will need to rewrite it as a servlet.
Richard...