使用 jfreechart 生成 曲线、柱状图、饼状图、分布图 展示到JSP

虽然现在JS做报表和图形展示已经非常普遍和漂亮了,但是不能忽略有jfreechart 这样一种东西!

这些翻阅资料,在看以前写的示例时发现了关于jfreechart 的简单示例,不管怎样发上来分享一下!

 

使用 jfreechart 生成 曲线、柱状图、饼状图、分布图 展示到JSP_第1张图片

 

这个示例使用JSP和Servlet做后台和前台展示,下面有源代码可以直接运行

生产线型趋势图:

 

Java代码   收藏代码
  1. package com.xidian.servlet;  
  2. import java.awt.Color;  
  3. import java.awt.Font;  
  4. import java.io.*;  
  5. import java.text.SimpleDateFormat;  
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10. import org.jfree.chart.*;  
  11. import org.jfree.chart.axis.DateAxis;  
  12. import org.jfree.chart.axis.ValueAxis;  
  13. import org.jfree.chart.plot.XYPlot;  
  14. import org.jfree.chart.renderer.xy.XYItemRenderer;  
  15. import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;  
  16. import org.jfree.chart.title.TextTitle;  
  17. import org.jfree.data.time.Day;  
  18. import org.jfree.data.time.TimeSeries;  
  19. import org.jfree.data.time.TimeSeriesCollection;  
  20. import org.jfree.data.xy.XYDataset;  
  21. /** 
  22.  * 生产线型趋势图 
  23.  * @说明  
  24.  * @author cuisuqiang 
  25.  * @version 1.0 
  26.  * @since 
  27.  */  
  28. @SuppressWarnings("serial")  
  29. public class LineServlet extends HttpServlet {  
  30.     @SuppressWarnings("deprecation")  
  31.     @Override  
  32.     protected void service(HttpServletRequest request,  
  33.             HttpServletResponse response) throws ServletException, IOException {  
  34.         response.setContentType("text/html");  
  35.         // 在Mysql中使用 select  
  36.         // year(accessdate),month(accessdate),day(accessdate),count(*)  
  37.         // 其中accessdate 是一个date类型的时间  
  38.         // 时间序列对象集合  
  39.         TimeSeriesCollection chartTime = new TimeSeriesCollection();  
  40.         // 时间序列对象,第1个参数表示时间序列的名字,第2个参数是时间类型,这里为天  
  41.         // 该对象用于保存前count天每天的访问次数  
  42.         TimeSeries timeSeries = new TimeSeries("日访问", Day.class);  
  43.         // 为了演示,直接拼装数据  
  44.         // Day的组装格式是day-month-year 访问次数  
  45.         timeSeries.add(new Day(112010), 50);  
  46.         timeSeries.add(new Day(212010), 47);  
  47.         timeSeries.add(new Day(312010), 82);  
  48.         timeSeries.add(new Day(412010), 95);  
  49.         timeSeries.add(new Day(512010), 104);  
  50.         timeSeries.add(new Day(612010), 425);  
  51.         chartTime.addSeries(timeSeries);  
  52.         XYDataset date = chartTime;  
  53.         try {  
  54.             // 使用ChartFactory来创建时间序列的图表对象  
  55.             JFreeChart chart = ChartFactory.createTimeSeriesChart(  
  56.                     "网站每天访问统计"// 图形标题  
  57.                     "日期"// X轴说明  
  58.                     "访问量"// Y轴说明  
  59.                     date, // 数据  
  60.                     true// 是否创建图例  
  61.                     true// 是否生成Tooltips  
  62.                     false // 是否生产URL链接  
  63.             );  
  64.             // 设置整个图片的背景色  
  65.             chart.setBackgroundPaint(Color.PINK);  
  66.             // 设置图片有边框  
  67.             chart.setBorderVisible(true);  
  68.             // 获得图表区域对象  
  69.             XYPlot xyPlot = (XYPlot) chart.getPlot();  
  70.             // 设置报表区域的背景色  
  71.             xyPlot.setBackgroundPaint(Color.lightGray);  
  72.             // 设置横 纵坐标网格颜色  
  73.             xyPlot.setDomainGridlinePaint(Color.GREEN);  
  74.             xyPlot.setRangeGridlinePaint(Color.GREEN);  
  75.             // 设置横、纵坐标交叉线是否显示  
  76.             xyPlot.setDomainCrosshairVisible(true);  
  77.             xyPlot.setRangeCrosshairVisible(true);  
  78.             // 获得数据点(X,Y)的render,负责描绘数据点  
  79.             XYItemRenderer xyItemRenderer = xyPlot.getRenderer();  
  80.             if (xyItemRenderer instanceof XYLineAndShapeRenderer) {  
  81.                 XYLineAndShapeRenderer xyLineAndShapeRenderer = (XYLineAndShapeRenderer) xyItemRenderer;  
  82.                 xyLineAndShapeRenderer.setShapesVisible(true); // 数据点可见  
  83.                 xyLineAndShapeRenderer.setShapesFilled(true); // 数据点是实心点  
  84.                 xyLineAndShapeRenderer.setSeriesFillPaint(0, Color.RED); // 数据点填充为蓝色  
  85.                 xyLineAndShapeRenderer.setUseFillPaint(true);// 将设置好的属性应用到render上  
  86.             }  
  87.             // 配置以下内容方可解决乱码问题  
  88.             // 配置字体  
  89.             Font xfont = new Font("宋体", Font.PLAIN, 12);    // X轴  
  90.             Font yfont = new Font("宋体", Font.PLAIN, 12);    // Y轴  
  91.             Font kfont = new Font("宋体", Font.PLAIN, 12);    // 底部  
  92.             Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题  
  93.             // 图片标题  
  94.             chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));  
  95.             // 底部  
  96.             chart.getLegend().setItemFont(kfont);             
  97.             // X 轴  
  98.             ValueAxis domainAxis = xyPlot.getDomainAxis();  
  99.             domainAxis.setLabelFont(xfont);// 轴标题  
  100.             domainAxis.setTickLabelFont(xfont);// 轴数值  
  101.             domainAxis.setTickLabelPaint(Color.BLUE); // 字体颜色  
  102.             // Y 轴  
  103.             ValueAxis rangeAxis = xyPlot.getRangeAxis();  
  104.             rangeAxis.setLabelFont(yfont);  
  105.             rangeAxis.setLabelPaint(Color.BLUE); // 字体颜色  
  106.             rangeAxis.setTickLabelFont(yfont);  
  107.             // 定义坐标轴上日期显示的格式  
  108.             DateAxis dateAxis = (DateAxis) xyPlot.getDomainAxis();  
  109.             // 设置日期格式  
  110.             dateAxis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));  
  111.             // 向客户端输出生成的图片  
  112.             ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,  
  113.                     chart, 500300null);  
  114.         } catch (Exception e) {  
  115.             e.printStackTrace();  
  116.         }  
  117.     }  
  118. }  

 

 

生产柱状图:

 

Java代码   收藏代码
  1. package com.xidian.servlet;  
  2. import java.awt.Color;  
  3. import java.awt.Font;  
  4. import java.io.IOException;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. import org.jfree.chart.ChartFactory;  
  10. import org.jfree.chart.ChartUtilities;  
  11. import org.jfree.chart.JFreeChart;  
  12. import org.jfree.chart.axis.CategoryAxis;  
  13. import org.jfree.chart.axis.NumberAxis;  
  14. import org.jfree.chart.plot.CategoryPlot;  
  15. import org.jfree.chart.plot.PlotOrientation;  
  16. import org.jfree.chart.renderer.category.BarRenderer;  
  17. import org.jfree.chart.title.TextTitle;  
  18. import org.jfree.data.category.DefaultCategoryDataset;  
  19.   
  20. /** 
  21.  * 生产柱状图 
  22.  * @说明  
  23.  * @author cuisuqiang 
  24.  * @version 1.0 
  25.  * @since 
  26.  */  
  27. @SuppressWarnings("serial")  
  28. public class PillarServlet extends HttpServlet {  
  29.     @Override  
  30.     protected void service(HttpServletRequest request,  
  31.             HttpServletResponse response) throws ServletException, IOException {  
  32.         response.setContentType("text/html");  
  33.         // 使用普通数据集  
  34.         DefaultCategoryDataset chartDate = new DefaultCategoryDataset();  
  35.         // 增加测试数据,第一个参数是访问量,最后一个是时间,中间是显示用不考虑  
  36.         chartDate.addValue(55"访问量""2010-01");  
  37.         chartDate.addValue(65"访问量""2010-02");  
  38.         chartDate.addValue(59"访问量""2010-03");  
  39.         chartDate.addValue(156"访问量""2010-04");  
  40.         chartDate.addValue(452"访问量""2010-05");  
  41.         chartDate.addValue(359"访问量""2010-06");  
  42.         try {  
  43.             // 从数据库中获得数据集  
  44.             DefaultCategoryDataset data = chartDate;  
  45.               
  46.             // 使用ChartFactory创建3D柱状图,不想使用3D,直接使用createBarChart  
  47.             JFreeChart chart = ChartFactory.createBarChart3D(  
  48.                     "网站月访问量统计"// 图表标题  
  49.                     "时间"// 目录轴的显示标签  
  50.                     "访问量"// 数值轴的显示标签  
  51.                     data, // 数据集  
  52.                     PlotOrientation.VERTICAL, // 图表方向,此处为垂直方向  
  53.                     // PlotOrientation.HORIZONTAL, //图表方向,此处为水平方向  
  54.                     true// 是否显示图例  
  55.                     true// 是否生成工具  
  56.                     false // 是否生成URL链接  
  57.                     );            
  58.             // 设置整个图片的背景色  
  59.             chart.setBackgroundPaint(Color.PINK);  
  60.             // 设置图片有边框  
  61.             chart.setBorderVisible(true);  
  62.             Font kfont = new Font("宋体", Font.PLAIN, 12);    // 底部  
  63.             Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题  
  64.             // 图片标题  
  65.             chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));  
  66.             // 底部  
  67.             chart.getLegend().setItemFont(kfont);  
  68.             // 得到坐标设置字体解决乱码  
  69.             CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();  
  70.             categoryplot.setDomainGridlinesVisible(true);  
  71.             categoryplot.setRangeCrosshairVisible(true);  
  72.             categoryplot.setRangeCrosshairPaint(Color.blue);  
  73.             NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();  
  74.             numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());  
  75.             BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();  
  76.             barrenderer.setBaseItemLabelFont(new Font("宋体", Font.PLAIN, 12));  
  77.             barrenderer.setSeriesItemLabelFont(1new Font("宋体", Font.PLAIN, 12));  
  78.             CategoryAxis domainAxis = categoryplot.getDomainAxis();           
  79.             /*------设置X轴坐标上的文字-----------*/  
  80.             domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));  
  81.             /*------设置X轴的标题文字------------*/  
  82.             domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));  
  83.             /*------设置Y轴坐标上的文字-----------*/  
  84.             numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));  
  85.             /*------设置Y轴的标题文字------------*/  
  86.             numberaxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));  
  87.             /*------这句代码解决了底部汉字乱码的问题-----------*/  
  88.             chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));  
  89.             // 生成图片并输出  
  90.             ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,  
  91.                     chart, 500300null);  
  92.         } catch (Exception e) {  
  93.             e.printStackTrace();  
  94.         }  
  95.     }  
  96. }  

 

 

生成饼状统计图:

 

Java代码   收藏代码
  1. package com.xidian.servlet;  
  2. import java.awt.Color;  
  3. import java.awt.Font;  
  4. import java.io.IOException;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. import org.jfree.chart.ChartUtilities;  
  10. import org.jfree.chart.JFreeChart;  
  11. import org.jfree.chart.plot.PiePlot3D;  
  12. import org.jfree.chart.title.TextTitle;  
  13. import org.jfree.data.general.DefaultPieDataset;  
  14. /** 
  15.  * 生成饼状统计图 
  16.  * @说明  
  17.  * @author cuisuqiang 
  18.  * @version 1.0 
  19.  * @since 
  20.  */  
  21. @SuppressWarnings("serial")  
  22. public class CakeServlet extends HttpServlet {  
  23.     protected void service(HttpServletRequest request,  
  24.             HttpServletResponse response) throws ServletException, IOException {  
  25.         response.setContentType("text/html");  
  26.         // 默认数据类型  
  27.         DefaultPieDataset dataType = new DefaultPieDataset();  
  28.         // 数据参数 内容,数量  
  29.         dataType.setValue("IE6"156);  
  30.         dataType.setValue("IE7"230);  
  31.         dataType.setValue("IE8"45);  
  32.         dataType.setValue("火狐"640);  
  33.         dataType.setValue("谷歌"245);  
  34.         try {  
  35.             DefaultPieDataset data = dataType;  
  36.             // 生成普通饼状图除掉 3D 即可  
  37.             // 生产3D饼状图  
  38.             PiePlot3D plot = new PiePlot3D(data);  
  39.             JFreeChart chart = new JFreeChart(  
  40.                     "用户使用的浏览器类型",            // 图形标题  
  41.                     JFreeChart.DEFAULT_TITLE_FONT, // 标题字体  
  42.                     plot,                          // 图标标题对象  
  43.                     true                           // 是否显示图例  
  44.             );  
  45.             // 设置整个图片的背景色  
  46.             chart.setBackgroundPaint(Color.PINK);  
  47.             // 设置图片有边框  
  48.             chart.setBorderVisible(true);  
  49.             // 配置字体  
  50.             Font kfont = new Font("宋体", Font.PLAIN, 12);    // 底部  
  51.             Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题  
  52.             // 图片标题  
  53.             chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));  
  54.             // 底部  
  55.             chart.getLegend().setItemFont(kfont);  
  56.             ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,  
  57.                     chart, 500300null);  
  58.         } catch (Exception e) {  
  59.             e.printStackTrace();  
  60.         }  
  61.     }  
  62. }  

 

 

柱状分布统计图:

 

Java代码   收藏代码
  1. package com.xidian.servlet;  
  2. import java.awt.Color;  
  3. import java.awt.Font;  
  4. import java.io.IOException;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. import org.jfree.chart.ChartFactory;  
  10. import org.jfree.chart.ChartUtilities;  
  11. import org.jfree.chart.JFreeChart;  
  12. import org.jfree.chart.axis.CategoryAxis;  
  13. import org.jfree.chart.axis.NumberAxis;  
  14. import org.jfree.chart.plot.CategoryPlot;  
  15. import org.jfree.chart.plot.PlotOrientation;  
  16. import org.jfree.chart.renderer.category.BarRenderer;  
  17. import org.jfree.chart.title.TextTitle;  
  18. import org.jfree.data.category.DefaultCategoryDataset;  
  19. /** 
  20.  * 柱状分布统计图 
  21.  * @说明  
  22.  * @author cuisuqiang 
  23.  * @version 1.0 
  24.  * @since 
  25.  */  
  26. @SuppressWarnings("serial")  
  27. public class ParagraphsServlet extends HttpServlet {  
  28.     protected void service(HttpServletRequest request,  
  29.             HttpServletResponse response) throws ServletException, IOException {  
  30.         response.setContentType("text/html");  
  31.         DefaultCategoryDataset dataTime = new DefaultCategoryDataset();  
  32.         // 这是一组数据  
  33.         dataTime.addValue(52"0-6""2010-1-1");  
  34.         dataTime.addValue(86"6-12""2010-1-1");  
  35.         dataTime.addValue(126"12-18""2010-1-1");  
  36.         dataTime.addValue(42"18-24""2010-1-1");  
  37.         // 这是一组数据  
  38.         dataTime.addValue(452"0-6""2010-1-2");  
  39.         dataTime.addValue(96"6-12""2010-1-2");  
  40.         dataTime.addValue(254"12-18""2010-1-2");  
  41.         dataTime.addValue(126"18-24""2010-1-2");  
  42.         // 这是一组数据  
  43.         dataTime.addValue(256"0-6""2010-1-3");  
  44.         dataTime.addValue(86"6-12""2010-1-3");  
  45.         dataTime.addValue(365"12-18""2010-1-3");  
  46.         dataTime.addValue(24"18-24""2010-1-3");  
  47.         try {  
  48.             DefaultCategoryDataset data = dataTime;  
  49.             // 使用ChartFactory创建3D柱状图,不想使用3D,直接使用createBarChart  
  50.             JFreeChart chart = ChartFactory.createBarChart3D(  
  51.                     "网站时间段访问量统计",   
  52.                     "时间",   
  53.                     "访问量",   
  54.                     data,  
  55.                     PlotOrientation.VERTICAL,   
  56.                     true,   
  57.                     false,   
  58.                     false  
  59.             );  
  60.             // 设置整个图片的背景色  
  61.             chart.setBackgroundPaint(Color.PINK);  
  62.             // 设置图片有边框  
  63.             chart.setBorderVisible(true);  
  64.             Font kfont = new Font("宋体", Font.PLAIN, 12);    // 底部  
  65.             Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题  
  66.             // 图片标题  
  67.             chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));  
  68.             // 底部  
  69.             chart.getLegend().setItemFont(kfont);  
  70.             // 得到坐标设置字体解决乱码  
  71.             CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();  
  72.             categoryplot.setDomainGridlinesVisible(true);  
  73.             categoryplot.setRangeCrosshairVisible(true);  
  74.             categoryplot.setRangeCrosshairPaint(Color.blue);  
  75.             NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();  
  76.             numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());  
  77.             BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();  
  78.             barrenderer.setBaseItemLabelFont(new Font("宋体", Font.PLAIN, 12));  
  79.             barrenderer.setSeriesItemLabelFont(1new Font("宋体", Font.PLAIN, 12));  
  80.             CategoryAxis domainAxis = categoryplot.getDomainAxis();           
  81.             /*------设置X轴坐标上的文字-----------*/  
  82.             domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));  
  83.             /*------设置X轴的标题文字------------*/  
  84.             domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));  
  85.             /*------设置Y轴坐标上的文字-----------*/  
  86.             numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));  
  87.             /*------设置Y轴的标题文字------------*/  
  88.             numberaxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));  
  89.             /*------这句代码解决了底部汉字乱码的问题-----------*/  
  90.             chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));    
  91.             ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,  
  92.                     chart, 500300null);  
  93.         } catch (Exception es) {  
  94.             es.printStackTrace();  
  95.         }  
  96.     }  
  97. }  

 

 

JSP上使用IMG图片来请求Servlet显示图片:

 

Java代码   收藏代码
  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  7. <html>  
  8.   <head>  
  9.     <base href="<%=basePath%>">  
  10.     <title></title>  
  11.     <meta http-equiv="pragma" content="no-cache">  
  12.     <meta http-equiv="cache-control" content="no-cache">  
  13.     <meta http-equiv="expires" content="0">      
  14.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  15.     <meta http-equiv="description" content="This is my page">  
  16.   </head>  
  17.   <body>  
  18.     <center>  
  19.         <img src="LineServlet">  
  20.         <img src="PillarServlet">  
  21.         <img src="ParagraphsServlet">  
  22.         <img src="CakeServlet">  
  23.     </center>  
  24.   </body>  
  25. </html>  

 

 

WEB.xml配置:

 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <filter>  
  7.         <filter-name>EncodingFilter</filter-name>  
  8.         <filter-class>org.filter.EncodingFilter</filter-class>  
  9.     </filter>  
  10.     <filter-mapping>  
  11.         <filter-name>EncodingFilter</filter-name>  
  12.         <url-pattern>/*</url-pattern>  
  13.     </filter-mapping>  
  14.     <servlet>  
  15.         <servlet-name>LineServlet</servlet-name>  
  16.         <servlet-class>com.xidian.servlet.LineServlet</servlet-class>  
  17.     </servlet>  
  18.     <servlet>  
  19.         <servlet-name>PillarServlet</servlet-name>  
  20.         <servlet-class>com.xidian.servlet.PillarServlet</servlet-class>  
  21.     </servlet>  
  22.     <servlet>  
  23.         <servlet-name>ParagraphsServlet</servlet-name>  
  24.         <servlet-class>  
  25.             com.xidian.servlet.ParagraphsServlet  
  26.         </servlet-class>  
  27.     </servlet>  
  28.     <servlet>  
  29.         <servlet-name>CakeServlet</servlet-name>  
  30.         <servlet-class>com.xidian.servlet.CakeServlet</servlet-class>  
  31.     </servlet>  
  32.     <servlet-mapping>  
  33.         <servlet-name>LineServlet</servlet-name>  
  34.         <url-pattern>/LineServlet</url-pattern>  
  35.     </servlet-mapping>  
  36.     <servlet-mapping>  
  37.         <servlet-name>PillarServlet</servlet-name>  
  38.         <url-pattern>/PillarServlet</url-pattern>  
  39.     </servlet-mapping>  
  40.     <servlet-mapping>  
  41.         <servlet-name>ParagraphsServlet</servlet-name>  
  42.         <url-pattern>/ParagraphsServlet</url-pattern>  
  43.     </servlet-mapping>  
  44.     <servlet-mapping>  
  45.         <servlet-name>CakeServlet</servlet-name>  
  46.         <url-pattern>/CakeServlet</url-pattern>  
  47.     </servlet-mapping>  
  48.     <welcome-file-list>  
  49.         <welcome-file>index.jsp</welcome-file>  
  50.     </welcome-file-list>  
  51. </web-app>  

 

 

为了解决乱码问题,我配置了一个过滤器:

 

Java代码   收藏代码
  1. package org.filter;  
  2. import java.io.IOException;  
  3. import javax.servlet.Filter;  
  4. import javax.servlet.FilterChain;  
  5. import javax.servlet.FilterConfig;  
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.ServletRequest;  
  8. import javax.servlet.ServletResponse;  
  9. /** 
  10.  * 处理乱码 
  11.  * @说明  
  12.  * @author cuisuqiang 
  13.  * @version 1.0 
  14.  * @since 
  15.  */  
  16. public class EncodingFilter implements Filter {  
  17.     public void destroy() {  
  18.     }  
  19.     public void doFilter(ServletRequest request, ServletResponse response,  
  20.             FilterChain chain) throws IOException, ServletException {  
  21.         request.setCharacterEncoding("gbk");  
  22.         response.setCharacterEncoding("gbk");  
  23.         chain.doFilter(request, response);  
  24.     }  
  25.     public void init(FilterConfig arg0) throws ServletException {  
  26.   
  27.     }  
  28. }  

 

 

 

请您到ITEYE网站看原创,谢谢!

你可能感兴趣的:(jfreechart)