hi
i am using net beans IDE. I need to generate a chart in servlet. My code is as follows:
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
public class Chartservlet extends HttpServlet
{
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doTestPieChart(request, response);
} // End Method
/**
* This method creates a test pie chart using internally
* generated data. This method can be used to test the
* basic JFreeChart setup and the basic servlet
* configuration.
*
* @param request The HttpServletRequest request,
* which contains lots of useful
* information about the request.
*
* @param response The HttpServletResponse object,
* which allows the output being sent
* back to be tailored.
*/
protected void doTestPieChart(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
OutputStream out = response.getOutputStream();
try
{
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Graphic Novels", 192);
dataset.setValue("History", 125);
dataset.setValue("Military Fiction", 236);
dataset.setValue("Mystery", 547);
dataset.setValue("Performing Arts", 210);
dataset.setValue("Science, Non-Fiction", 70);
dataset.setValue("Science Fiction", 989);
JFreeChart chart = ChartFactory.createPieChart(
"Books by Type", // Title
dataset, // Data
true, // Yes, display the legend
false, // No, don't display tooltips
false // No, no URLs
);
chart.setBackgroundPaint(Color.white);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
}
catch (Exception e)
{
System.out.println(e.toString());
}
finally
{
out.close();
}
} // End Method
} // End Class
But when i run the servlet it shows an error like :class chartservlet neither has a main method nor it is a servlet specified in web.xml
I went through the developer guide and found that server.jar file must be included.
1)How will I include that in net beans. Should I install Tomcat for this purpose?
2)Is there any other procedures I should do before running a servlet with JFreechart?
3)Or is something wrong in the code given above
Anykind of help would be really appreciated.
Thanks in advance
netbeans and JFreechart
Re: netbeans and JFreechart
Hi!
Did you configure your servlet in web.xml? You can find an example here or use google. As I told you yesterday in another post, please don't ask servlet specific questions here. There are better places to ask these kind of questions. Same goes for netbeans. I don't use netbeans, so I can't help.
Try starting with a "hello world"-servlet. If this is working, try the code you posted for getting a chart. The code looks fine.
hth,
- martin
Did you configure your servlet in web.xml? You can find an example here or use google. As I told you yesterday in another post, please don't ask servlet specific questions here. There are better places to ask these kind of questions. Same goes for netbeans. I don't use netbeans, so I can't help.
Try starting with a "hello world"-servlet. If this is working, try the code you posted for getting a chart. The code looks fine.
hth,
- martin
Re: netbeans and JFreechart
Thanks for the reply.
Yes,
I tried configuring xml file also. But I get the same error msg.
Yes,
I tried configuring xml file also. But I get the same error msg.