浏览 2122 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-09-25
1,在Web程序中加入jcommon-1.0.0.jar和jfreechart-1.0.1.jar 2,在web.xml中加入jfreeChart的配置: <!-- JFreeChart访问出成的图片的Serlvet --> <servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/DisplayChart</url-pattern> </servlet-mapping> 3,新建一个我们自己的访问业务逻辑的Servlet如下: package com.mengya.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.servlet.ServletUtilities; import org.jfree.data.general.DefaultPieDataset; /** * 由JFreeChart生成图片在Web中访问 * 对于Web程序可以由org.jfree.chart.servlet.ServletUtilities类完成图片的生成 * @author 张明学 * */ @SuppressWarnings("serial") public class MyjfreeChartServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DefaultPieDataset dpd = new DefaultPieDataset(); dpd.setValue("管理人员", 25); dpd.setValue("市场人员", 10); dpd.setValue("开发人员", 50); dpd.setValue("其它人员", 15); JFreeChart chart = ChartFactory.createPieChart("某公司组织结构图", dpd, true, false, false); /** * 图片放在服务临时目录中,对tomcat是在X\Apache Software Foundation\Tomcat 6.0\temp */ String fileName=ServletUtilities.saveChartAsJPEG(chart,800,600,request.getSession()); /** * 出生成web页面访问图片的url,该url由org.jfree.chart.servlet.DisplayChart负责 * org.jfree.chart.servlet.DisplayChart是一个Serlvet已经配置在web.xml中了 * 所以在这个就可以直接引用该Serlvet配置的路径 * 即“/DisplayChart?fileName=”是固定的除非你把org.jfree.chart.servlet.DisplayChart这个Serlvet的访问路径改成别的了 * org.jfree.chart.servlet.DisplayChart中的部分代码如下: * HttpSession session = request.getSession(); * String filename = request.getParameter("filename"); * * if (filename == null) { * throw new ServletException("Parameter 'filename' must be supplied"); * } * * // Replace ".." with "" * // This is to prevent access to the rest of the file system * filename = ServletUtilities.searchReplace(filename, "..", ""); * * // Check the file exists * File file = new File(System.getProperty("java.io.tmpdir"), filename); * if (!file.exists()) { * throw new ServletException( * "File '" + file.getAbsolutePath() + "' does not exist" * ); * } */ String url=request.getContextPath()+"/DisplayChart?filename="+fileName; request.setAttribute("jfreeChartUrl", url); request.getRequestDispatcher("jfreeChart.jsp").forward(request, response); } } 4,在web.xml中配置我们自己的Serlvet如下: <!-- 自己的serlvet --> <servlet> <servlet-name>MyjfreeChartServlet</servlet-name> <servlet-class> com.mengya.servlet.MyjfreeChartServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>MyjfreeChartServlet</servlet-name> <url-pattern>/MyjfreeChartServlet</url-pattern> </servlet-mapping> 5,在我们的jsp中访问我们的Serlvet如下: index.jsp <h3 align="center"><a href="./MyjfreeChartServlet">出成图片</a></h3> jfreeChart.jsp <div align="center"> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |