JFreeChart Servlet Example Tutorial - Pie Chart Java - Part 2

In Part 1 of the post, we created a Java Servlet and compiled it. This Java Servlet was designed to accept an incoming request and return a pie chart as an image, after creating it in JFreeChart API. We have the Java Code available with us and in this post, we will see how to deploy this servlet and load the necessary supporting Jar files and see our JFreeChart Servlet in action. Follow the steps below to deploy our chart servlet to Tomcat server; [ Note: A JSP servlet can also be written on the same lines ]

Step-4: To run a servlet that creates a chart file using JFreeChart, you will have to copy the required Jar files to apache-tomcat-7.0.14\lib folder. Copy the following Jar files to the lib folder, otherwise, the compiled code will not execute and will throw a runtime exception;
jfreechart-1.0.13.jar
jcommon-1.0.16.jar
Step-5: Once you have loaded the required Jar files, you can either create a new application or update an existing application inside tomcat so that we can run the pie chart example through it. For this tutorial, we will update the existing "example" application available under apache-tomcat-7.0.14\webapps\examples to support the class file we have created. So, navigate to apache-tomcat-7.0.14\webapps\examples\WEB-INF and click on edit against the web.xml file. We will have to add some lines and you can follow the sample provided below to do this.
    <servlet>
        <servlet-name>HelloWorldExample</servlet-name>
        <servlet-class>HelloWorldExample</servlet-class>
    </servlet>
    <servlet> <!-- add a line for the PieChartServlet, similar to HellWorld example --> 
        <servlet-name>PieChartServlet</servlet-name>
        <servlet-class>PieChartServlet</servlet-class>
    </servlet>
Step-6: You should now create a URL pattern inside web.xml so that Tomcat knows that it has to invoke the class file if the URL pattern matches with the one invoked from the client. Follow the example provided below to create a URL pattern in your web.xml file;
    <servlet-mapping>
        <servlet-name>HelloWorldExample</servlet-name>
        <url-pattern>/servlets/servlet/HelloWorldExample</url-pattern>
    </servlet-mapping>
    <servlet-mapping><!-- Create a URL pattern for the new pie chart servlet -->
        <servlet-name>PieChartServlet</servlet-name>
        <url-pattern>/servlets/servlet/PieChartServlet</url-pattern>
    </servlet-mapping>
Step-7: Save the web.xml file once these steps are completed. You have to restart your web server after this step so that it picks up the updated xml file and its new definitions.
Step-8: Once this is done, point your browser to the following location (assuming you have webserver in your own machine and in port 8080)
http://localhost:8080/examples/servlets/servlet/PieChartServlet
If you have done all the steps correctly, you will get a good pie chart on your browser, as shown below;
JFreeChart Servlet Example Java
JFreeChart Servlet Example - Output from Servlet
A 16kb PNG Image file was returned from our chart servlet as a response; great it works!

No comments:

Post a Comment