servlet
try to do like following
1.by JDBC get the data from your database (like mysql, sqlserver ...).
2.you may code a javabean to make a jfreechart object and make a
public method like following
......................
public void drawMyChart(OutputStream x) { .....
JFreeChart chart = ............
ChartUtilities.writeChartAsJPEG(x, 100, chart, 800, 600);};
3.then you code a servlet and call method drawMyChart()..like following
............................
response.setContentType("image/jpeg");
drawMyChart(response.getOupPutStream);
....................................
2.you may code a javabean to make a jfreechart object and make a
public method like following
......................
public void drawMyChart(OutputStream x) { .....
JFreeChart chart = ............
ChartUtilities.writeChartAsJPEG(x, 100, chart, 800, 600);};
3.then you code a servlet and call method drawMyChart()..like following
............................
response.setContentType("image/jpeg");
drawMyChart(response.getOupPutStream);
....................................
Still can not display chart using JfreeChart Servlet
Hi Dennislee
I follow your hints but I can not display the chart using jfreechart servlet. Can you explain it more details.
Thanx
Winne

Thanx
Winne
Hi,
Garv, below is my code (servlet, using JBuilder) :
public class Servlet2 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DisplayChart dc = new DisplayChart();
.
.
.
.
and the error : class not found.
FYI : I am newbie in java.
Thanx,
Winne
Garv, below is my code (servlet, using JBuilder) :
public class Servlet2 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DisplayChart dc = new DisplayChart();
.
.
.
.
and the error : class not found.
FYI : I am newbie in java.
Thanx,
Winne
-
- Posts: 115
- Joined: Fri Mar 14, 2003 3:13 pm
- Location: London, England
- Contact:
DisplayChart Servlet
That's not the way to use the DisplayChart servlet. Check out the sample WAR file linked at the top of the forum to see how to use it correctly.
Regards,
Richard...
Regards,
Richard...
hi please try if like following ways
hi
please try do it like this
/**
*this class file for create your chart
*/
package helloservlet;
import java.io.*;
import org.jfree.chart.*;
import org.jfree.data.*;
public class myDemoChart {
public myDemoChart() {
}
private DefaultPieDataset getDataSet() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("C++", 700);
dataset.setValue("JAVA", 300);
dataset.setValue("Delphi", 200);
dataset.setValue("ada", 400);
dataset.setValue("smalltalk", 50);
dataset.setValue("perl", 100);
dataset.setValue("php", 120);
return dataset;
}
public void getchart(OutputStream resp) {
DefaultPieDataset data = getDataSet();
JFreeChart chart = ChartFactory.createPie3DChart("program language",
data, true, false, false);
try {
ChartUtilities.writeChartAsJPEG(resp, 100, chart, 700, 500, null);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* this class file is a servlet
*/
package helloservlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Servlet1 extends HttpServlet {
private static final String CONTENT_TYPE = "image/jpeg";
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
myDemoChart tmp = new myDemoChart();
tmp.getchart(response.getOutputStream()) ;
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
//Process the HTTP Put request
public void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
//Clean up resources
public void destroy() {
}
}
/**
* this is a jsp file
*/
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
jsp1
</title>
</head>
<jsp:useBean id="myDemoChart" scope="session" class="helloservlet.myDemoChart" />
<jsp:setProperty name="jsp1BeanId" property="*" />
<body bgcolor="#ffffff">
<h1>
hello jfreeChart
</h1>
<%
response.setContentType("image/jpeg");
myDemoChart.getchart(response.getOutputStream());
%>
<form method="post">
<br>Enter new value : <input name="sample"><br>
<br><br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
<br>
Value of Bean property is :<jsp:getProperty name="jsp1BeanId" property="sample" />
</form>
</body>
</html>
please try do it like this
/**
*this class file for create your chart
*/
package helloservlet;
import java.io.*;
import org.jfree.chart.*;
import org.jfree.data.*;
public class myDemoChart {
public myDemoChart() {
}
private DefaultPieDataset getDataSet() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("C++", 700);
dataset.setValue("JAVA", 300);
dataset.setValue("Delphi", 200);
dataset.setValue("ada", 400);
dataset.setValue("smalltalk", 50);
dataset.setValue("perl", 100);
dataset.setValue("php", 120);
return dataset;
}
public void getchart(OutputStream resp) {
DefaultPieDataset data = getDataSet();
JFreeChart chart = ChartFactory.createPie3DChart("program language",
data, true, false, false);
try {
ChartUtilities.writeChartAsJPEG(resp, 100, chart, 700, 500, null);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* this class file is a servlet
*/
package helloservlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Servlet1 extends HttpServlet {
private static final String CONTENT_TYPE = "image/jpeg";
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
myDemoChart tmp = new myDemoChart();
tmp.getchart(response.getOutputStream()) ;
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
//Process the HTTP Put request
public void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
//Clean up resources
public void destroy() {
}
}
/**
* this is a jsp file
*/
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
jsp1
</title>
</head>
<jsp:useBean id="myDemoChart" scope="session" class="helloservlet.myDemoChart" />
<jsp:setProperty name="jsp1BeanId" property="*" />
<body bgcolor="#ffffff">
<h1>
hello jfreeChart
</h1>
<%
response.setContentType("image/jpeg");
myDemoChart.getchart(response.getOutputStream());
%>
<form method="post">
<br>Enter new value : <input name="sample"><br>
<br><br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
<br>
Value of Bean property is :<jsp:getProperty name="jsp1BeanId" property="sample" />
</form>
</body>
</html>
-
- Posts: 115
- Joined: Fri Mar 14, 2003 3:13 pm
- Location: London, England
- Contact:
Bad example
That is not how to do it. Your HTML and chart images need to be transferred in two separate HTTP responses, not one. See the sample WAR file for more information.
Regards,
Richard...
Regards,
Richard...
i have a little problem
hi richard
first thank you very much , after saw your WAR demo , i think that your
way is right , but i think if we save a chart into a tmp directoy, our web
application's performance is badly, when the most clients access web
server, i think that access disk is not a good idea, is't it?
first thank you very much , after saw your WAR demo , i think that your
way is right , but i think if we save a chart into a tmp directoy, our web
application's performance is badly, when the most clients access web
server, i think that access disk is not a good idea, is't it?
You needn't save the chart to disk, you can use a servlet to send it to the client directly, precisely the way you're doing in the JSP file you posted. This bit:way is right , but i think if we save a chart into a tmp directoy, our web
application's performance is badly, when the most clients access web
server, i think that access disk is not a good idea, is't it?
Code: Select all
response.setContentType("image/jpeg");
myDemoChart.getchart(response.getOutputStream());
As a final note, you should strongly consider using PNG encoding for the chart, not JPEG encoding. The latter isn't very suitable for the kind of images (with large areas of a single colour) that JFreeChart creates.
can you post a demo about two respones
hi
thank you
can you post a demo about two response one is for html the other is for chart in jsp /servlet
thank you
can you post a demo about two response one is for html the other is for chart in jsp /servlet
I won't go into details - I'm at work. You'll have to fiddle around a bit to make this work.
The key thing to understand here is that from the browser's point of view calling this servlet is exactly the same as calling a normal png image. Therefore, you can use the image in any HTML code (generally a JSP, but it could just as well be a static HTML file) with the normal <img ...> tag.
Code: Select all
// some servlet, for this example consider it mapped to [b]/myapp/ChartServlet[/b] in web.xml
public void doGet( ... )
{
// set up your dataset here
CategoryDataset blah = whatever();
// set up the JFreeChart
JFreeChart jfc = chartfactory.whateverChart(blah);
// set the reponse content type
response.setContentType("text/png");
// send the response
ChartUtilities.writeChartAsPNG(
response.getOutputStream(),
jfc, 640, 480);
// close the output stream
response.getOutputStream().close();
}
Code: Select all
<img src="http://myserver/myapp/ChartServlet" alt="my very impressive chart"/>
that's a good way
HI
Dear Grav
thank you , your way is good:)
Dear Grav
thank you , your way is good:)
Not able to compile the servlet
hi,
I seen the code above tried. Iam able to compile the java code(myDemoChart.java) and not able to compile the servlet file.
And I tried to run the jsp page, But it shows that classnt found Exception for the Pie Dataset.
Can you pls show me the procedures to run a simple chart in JSP.
Iam in the deadline of my project.
thanx
Balaji
I seen the code above tried. Iam able to compile the java code(myDemoChart.java) and not able to compile the servlet file.
And I tried to run the jsp page, But it shows that classnt found Exception for the Pie Dataset.
Can you pls show me the procedures to run a simple chart in JSP.
Iam in the deadline of my project.
thanx
Balaji