Step 1 :
HTML:
MyHtml.html include a button, this button submit a request "draw" to "ChartServlet" servlet, "ChartServlet" will give a response.
MyHtml.html is like this:
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>MyHtml.html</title>
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="this is my page">
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
- </head>
- <body><h3>Draw a Chat !</h3><br>
- <form action="draw.do" method="get" name ="myform">
- <input type="submit" name="b1" value="draw"></input>
- </form>
- </body>
- </html>
Step 2:
modifY web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
- <description>This is the description of my J2EE component</description>
- <display-name>This is the display name of my J2EE component</display-name>
- <servlet-name>ChartServlet</servlet-name>
- <servlet-class>ChartServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>ChartServlet</servlet-name>
- <url-pattern>/draw.do</url-pattern>
- </servlet-mapping>
- </web-app>
Step 3:
ChartServlet recieves the request, then response it with the type "image/png" like this
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.jfree.chart.ChartFactory;
- import org.jfree.chart.ChartUtilities;
- import org.jfree.chart.JFreeChart;
- import org.jfree.data.general.DefaultPieDataset;
- import com.lowagie.text.pdf.codec.Base64.OutputStream;
- public class ChartServlet extends HttpServlet {
- /**
- * Constructor of the object.
- */
- public ChartServlet() {
- super();
- }
- /**
- * Destruction of the servlet. <br>
- */
- public void destroy() {
- super.destroy(); // Just puts "destroy" string in log
- // Put your code here
- }
- /**
- * The doGet method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to get.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- ServletOutputStream out = response.getOutputStream();
- // createadataset...
- DefaultPieDataset dataset = new DefaultPieDataset();
- dataset.setValue("Category1", 43.2);
- dataset.setValue("Category2", 27.9);
- dataset.setValue("Category3", 79.5);
- // createachart...
- JFreeChart chart = ChartFactory.createPieChart("SamplePieChart",
- dataset, true,// legend?
- true,// tooltips?
- false// URLs?
- );
- response.setContentType("image/png");
- ChartUtilities.writeChartAsPNG(out,chart,400,300);
- out.close();
- }
- /**
- * Initialization of the servlet. <br>
- *
- * @throws ServletException if an error occure
- */
- public void init() throws ServletException {
- // Put your code here
- }
- }
This servlet just do one simple thing, when request whose name is "draw" comes, the servlet will draw a chart(with JFreeChart support), the result is a image file as png, then, it adds this file to the response stream., which will output by the browser
Here we go!
Contact me! Tony80plus@gmail.com