servlet

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
turfman1

servlet

Post by turfman1 » Fri Mar 05, 2004 10:39 pm

has anyone run into concurrency issues when using DisplayChart servlet ?
If so, what did you do to resolve it ?

dennislee
Posts: 13
Joined: Mon Feb 23, 2004 3:02 am
Location: beijing, china

try to do like following

Post by dennislee » Mon Mar 08, 2004 3:25 am

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);
....................................

Winne

Still can not display chart using JfreeChart Servlet

Post by Winne » Thu Apr 01, 2004 4:33 am

Hi Dennislee

:( I follow your hints but I can not display the chart using jfreechart servlet. Can you explain it more details.

Thanx
Winne

garv
Posts: 127
Joined: Wed Mar 31, 2004 3:44 pm
Location: Amsterdam, The Netherlands

Post by garv » Thu Apr 01, 2004 9:09 am

Are you getting an Exception? If so, what's the message?

It would also be helpful if you posted the relevant parts of your source code.

Guest

Post by Guest » Thu Apr 01, 2004 11:03 am

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

richard_atkinson
Posts: 115
Joined: Fri Mar 14, 2003 3:13 pm
Location: London, England
Contact:

DisplayChart Servlet

Post by richard_atkinson » Thu Apr 01, 2004 8:47 pm

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...

dennislee
Posts: 13
Joined: Mon Feb 23, 2004 3:02 am
Location: beijing, china

hi please try if like following ways

Post by dennislee » Mon Apr 05, 2004 2:12 am

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>

richard_atkinson
Posts: 115
Joined: Fri Mar 14, 2003 3:13 pm
Location: London, England
Contact:

Bad example

Post by richard_atkinson » Mon Apr 05, 2004 12:45 pm

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...

dennislee
Posts: 13
Joined: Mon Feb 23, 2004 3:02 am
Location: beijing, china

i have a little problem

Post by dennislee » Tue Apr 06, 2004 4:02 am

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?

garv
Posts: 127
Joined: Wed Mar 31, 2004 3:44 pm
Location: Amsterdam, The Netherlands

Post by garv » Tue Apr 06, 2004 8:34 am

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:

Code: Select all

response.setContentType("image/jpeg");
myDemoChart.getchart(response.getOutputStream()); 
What you can not do is send HTML and chart data at the same time. You need to write a servlet or JSP to (only) send HTML, and a seperate servlet to (only) send chart data. The disk is never used.

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.

dennislee
Posts: 13
Joined: Mon Feb 23, 2004 3:02 am
Location: beijing, china

can you post a demo about two respones

Post by dennislee » Wed Apr 07, 2004 6:18 am

hi

thank you

can you post a demo about two response one is for html the other is for chart in jsp /servlet

garv
Posts: 127
Joined: Wed Mar 31, 2004 3:44 pm
Location: Amsterdam, The Netherlands

Post by garv » Wed Apr 07, 2004 8:35 am

I won't go into details - I'm at work. You'll have to fiddle around a bit to make this work.

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();
}
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

<img src="http://myserver/myapp/ChartServlet" alt="my very impressive chart"/>

dennislee
Posts: 13
Joined: Mon Feb 23, 2004 3:02 am
Location: beijing, china

that's a good way

Post by dennislee » Wed Apr 07, 2004 10:15 am

HI
Dear Grav

thank you , your way is good:)

balaji

Not able to compile the servlet

Post by balaji » Fri Apr 30, 2004 10:35 am

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

amit

Post by amit » Wed May 12, 2004 3:23 pm

can you tell where i will get that sample war file

thanx
rohan

Locked