How to invoke a servlet generating an image ?

F

fabien.moquet

Hello,

I am using a Servlet that generates a PNG image using JFreeChart on a
Tomcat 6.0 server.

I would like to test the servlet but in don't know how to invoke it
from my brower. I tried to open the URL http://localhost:8080/myapp/ServletBarChart
but Tomcat displays an error saying that the file could not be found.

Here is my servlet file :

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.*;
import org.jfree.data.category.*;

public class ServletBarChart extends HttpServlet {

protected void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(120000.0, "Produit 1", "2000");
dataset.addValue(550000.0, "Produit 1", "2001");
dataset.addValue(180000.0, "Produit 1", "2002");
dataset.addValue(270000.0, "Produit 2", "2000");
dataset.addValue(600000.0, "Produit 2", "2001");
dataset.addValue(230000.0, "Produit 2", "2002");
dataset.addValue(90000.0, "Produit 3", "2000");
dataset.addValue(450000.0, "Produit 3", "2001");
dataset.addValue(170000.0, "Produit 3", "2002");

JFreeChart barChart = ChartFactory.createBarChart("Evolution des
ventes", "",
"Unite vendue", dataset, PlotOrientation.VERTICAL, true, true,
false);
OutputStream out = response.getOutputStream();
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, barChart, 400, 300);
}
}

What am I doing wrong ?

Fabzy
 
N

Nino

Hello,

I am using a Servlet that generates a PNG image using JFreeChart on a
Tomcat 6.0 server.

I would like to test the servlet but in don't know how to invoke it
from my brower. I tried to open the URLhttp://localhost:8080/myapp/ServletBarChart
but Tomcat displays an error saying that the file could not be found.

Perhaps you do not have Tomcat set up correctly? Where did you put the
compiled file? Did you properly set up the configuration for that
folder? Is your "myapp" context set to reload when a new file is added
or do you need to restart Tomcat?

I have only worked with Tomcat 4 (and a little with 5), but I'm
assuming they are fairly similar. Everything else looks okay.

Nino
 
C

Carl

Hello,

I am using a Servlet that generates a PNG image using JFreeChart on a
Tomcat 6.0 server.

I would like to test the servlet but in don't know how to invoke it
from my brower. I tried to open the URLhttp://localhost:8080/myapp/ServletBarChart
but Tomcat displays an error saying that the file could not be found.

Here is my servlet file : --Code cut out--

What am I doing wrong ?

Hello,

What does your web.xml look like and where is your servlet located?

For testing, I would place the servlet class in:
(note: you should really define a package for your servlet)

myapp/WEB-INF/classes/ServletBarChart.class

And add the following to your web.xml inside the <web-app> block:

<servlet>
<servlet-name>TestImage</servlet-name>
<servlet-class>ServletBarChart</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>TestImage</servlet-name>
<url-pattern>/showImage.do</url-pattern>
</servlet-mapping>

Restart the server, and you should be able to see your image using the
url:
http://localhost:8080/myapp/showImage.do

Hope that helps,
Carl.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top