利用JFreeChart在browser中绘图(Servlet)

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:

 

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3.   <head>
  4.     <title>MyHtml.html</title>
  5.     
  6.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  7.     <meta http-equiv="description" content="this is my page">
  8.     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  9.     
  10.     <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  11.   </head>
  12.   
  13.   <body><h3>Draw a Chat !</h3><br>
  14.     <form action="draw.do" method="get" name ="myform">
  15.     <input type="submit" name="b1" value="draw"></input>
  16.     </form>
  17.   </body>
  18. </html>

Step 2:

modifY web.xml

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.4" 
  3.     xmlns="http://java.sun.com/xml/ns/j2ee" 
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  7.   <servlet>
  8.     <description>This is the description of my J2EE component</description>
  9.     <display-name>This is the display name of my J2EE component</display-name>
  10.     <servlet-name>ChartServlet</servlet-name>
  11.     <servlet-class>ChartServlet</servlet-class>
  12.   </servlet>
  13.   <servlet-mapping>
  14.     <servlet-name>ChartServlet</servlet-name>
  15.     <url-pattern>/draw.do</url-pattern>
  16.   </servlet-mapping>
  17. </web-app>

Step 3:

ChartServlet recieves the request, then response it with the type "image/png" like this

 

  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.ServletOutputStream;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.jfree.chart.ChartFactory;
  9. import org.jfree.chart.ChartUtilities;
  10. import org.jfree.chart.JFreeChart;
  11. import org.jfree.data.general.DefaultPieDataset;
  12. import com.lowagie.text.pdf.codec.Base64.OutputStream;
  13. public class ChartServlet extends HttpServlet {
  14.     /**
  15.      * Constructor of the object.
  16.      */
  17.     public ChartServlet() {
  18.         super();
  19.     }
  20.     /**
  21.      * Destruction of the servlet. <br>
  22.      */
  23.     public void destroy() {
  24.         super.destroy(); // Just puts "destroy" string in log
  25.         // Put your code here
  26.     }
  27.     /**
  28.      * The doGet method of the servlet. <br>
  29.      *
  30.      * This method is called when a form has its tag value method equals to get.
  31.      * 
  32.      * @param request the request send by the client to the server
  33.      * @param response the response send by the server to the client
  34.      * @throws ServletException if an error occurred
  35.      * @throws IOException if an error occurred
  36.      */
  37.     public void doGet(HttpServletRequest request, HttpServletResponse response)
  38.             throws ServletException, IOException {
  39.         ServletOutputStream out = response.getOutputStream(); 
  40. //       createadataset...
  41.         DefaultPieDataset dataset = new DefaultPieDataset();
  42.         dataset.setValue("Category1"43.2);
  43.         dataset.setValue("Category2"27.9);
  44.         dataset.setValue("Category3"79.5);
  45.         // createachart...
  46.         JFreeChart chart = ChartFactory.createPieChart("SamplePieChart",
  47.                 dataset, true,// legend?
  48.                 true,// tooltips?
  49.                 false// URLs?
  50.                 );
  51.         response.setContentType("image/png");
  52.         ChartUtilities.writeChartAsPNG(out,chart,400,300);
  53.         out.close();
  54.     }
  55.     /**
  56.      * Initialization of the servlet. <br>
  57.      *
  58.      * @throws ServletException if an error occure
  59.      */
  60.     public void init() throws ServletException {
  61.         // Put your code here
  62.     }
  63. }

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值