Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

How to invoke a servlet generating an image ?

Thread view: 
fabien.moquet@gmail.com - 17 May 2007 10:33 GMT
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
Nino - 17 May 2007 18:56 GMT
On May 17, 2:33 am, fabien.moq...@gmail.com wrote:
> Hello,
>
[quoted text clipped - 4 lines]
> 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
Carl - 17 May 2007 23:16 GMT
On May 17, 2:33 am, fabien.moq...@gmail.com wrote:
> Hello,
>
[quoted text clipped - 6 lines]
>
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.