如何使用JFreeChart在外部Tomcat中运行报表?

时间:2015-06-30 02:35:36

标签: java tomcat

我刚用JFreeChart创建了报告,它在Tomcat中运行,它嵌入在MyEclipse中。但是,当我尝试在外部Tomcat中创建报表并在浏览器(例如Firefox)上显示报表时,它会失败。 Tomcats日志显示以下错误:

org.apache.catalina.loader.WebappClassLoader findResourceInternal
Message: Illegal access: this web application instance has been stopped already.
Could not load ehcache-version.properties. 
The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.

以下是我的报告代码:

JSP页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <script type="text/javascript" src="./js/jquery.min.js"></script>
    <script type="text/javascript" src="./js/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="./js/locale/easyui-lang-zh_CN.js"></script>
    <link rel="stylesheet" type="text/css" href="./js/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="./js/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="./js/demo.css">

  </head>

  <body>
Select start time
    <input id="startTime" class="easyui-datetimebox" required style="width:200px">
Select end time

    <input id="endTime" class="easyui-datetimebox" required style="width:200px">
    <button id="show" onclick="showChart()">Go</button>
    <label id="info"></label><br/><br/>
    <div id="chart"></div>
    <div id="p" style="width:400px;"></div> 

    <script type="text/javascript">
        function showChart(){

            //Get the start time
            var startTime = $('#startTime').datetimebox('getValue');
            var endTime = $('#endTime').datetimebox('getValue');    
            if(startTime==null || startTime=="" || endTime==null || endTime==""){
                alert('Invalid date');
            }else{
                $('#info').empty();
                $('#info').append("<b>Please wait</b>");
                //Refresh the chart
                $('#chart').empty();
                $('#chart').append("<img src=\"jFreeCharTest?startTime="+startTime+"&endTime="+endTime+"\"></img>");
            }            

        }

        function ajaxLoading(){ 
            $("<div class=\"datagrid-mask\"></div>").css({display:"block",width:"100%",height:$(window).height()}).appendTo("body"); 
            $("<div class=\"datagrid-mask-msg\"></div>").html("Please wait").appendTo("body").css({display:"block",left:($(document.body).outerWidth(true) - 190) / 2,top:($(window).height() - 45) / 2}); 
        } 
        function ajaxLoadEnd(){ 
             $(".datagrid-mask").remove(); 
             $(".datagrid-mask-msg").remove();             
        } 
    </script>
  </body>
</html>

数据来源:

 import java.sql.Connection;
    import java.sql.DriverManager;

    public class DB {
        private static final String driverName = "com.mysql.jdbc.Driver";
        private static final String url="jdbc:mysql://**/**?user=**&password=**";
        public static Connection getConn(){
            Connection conn = null;
            try {
                Class.forName(driverName);
                conn = DriverManager.getConnection(url);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return conn;
        }

}

数据集和查询:

public class JFreeCharTest extends HttpServlet {

    public JFreeCharTest() {
        super();
    }

    public void init() throws ServletException {

    }

    public void destroy() {
        super.destroy(); 
    }

    @Override  
    protected void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException,  
            IOException {  
        doGet(request, response);  
    }  

    @Override  
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  

        response.setContentType("image/jpeg");  
        OutputStream out = response.getOutputStream();  
        //Get the parameter
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        //get the result
        CategoryDataset ds = getDataSet(startTime,endTime);  
        JFreeChart chart = ChartFactory.createBarChart3D(  
                "The total number of TTC buses", //Graph title 
                "The TTC routes", //X-Label 
                "The number of TTC buses", //Y-Label  
                ds, //dataset 
                PlotOrientation.VERTICAL, //Orientation of graph  
                true, //Generating example
                false, //Generating tool 
                false);         //Generating url 

        CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();  

        NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();  

        CategoryAxis domainAxis = categoryplot.getDomainAxis();  

        /*title in x coordinate*/  
        domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));  

        /*the font style in x coordinate*/  
        domainAxis.setLabelFont(new Font("Times New Roman", Font.PLAIN, 12));  

        /*title in y coordinate*/  
        numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));  

        /*font style in y coordinate*/  
        numberaxis.setLabelFont(new Font("Times New Roman", Font.PLAIN, 12));  


        chart.getLegend().setItemFont(new Font("Times New Roman", Font.PLAIN, 12));  


        chart.getTitle().setFont(new Font("Times New Roman", Font.PLAIN, 12));  

        try {  
            ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 1240, 400, null);  
        } finally {  
            try {  
                out.close();  
            } catch (Exception ex) {  
                ex.printStackTrace();  
            }  
        }  
    }  



    private CategoryDataset getDataSet(String startTime,String endTime) { 
        DefaultCategoryDataset ds = new DefaultCategoryDataset(); 
        try{
            //Connecting database
            Connection cn = DB.getConn();
            System.out.println("Searching");
            String sql = "select count(distinct ttc.vehicle_id) as vc,ttc.routeTag,ttc_routes.title,ttc.dateTime from ttc INNER JOIN ttc_routes ON ttc.routeTag = ttc_routes.routeTag WHERE ttc.dateTime >= '"+startTime+"' and ttc.dateTime <= '"+endTime+"' GROUP BY ttc_routes.title order by vc desc limit 10";
            PreparedStatement ps = cn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            System.out.println("Searching end");
            while(rs.next()){
                ds.addValue(rs.getInt(1), rs.getString(3), rs.getString(3));
            }
        }catch(Exception e){
            e.printStackTrace();
        }


        return ds;  
    }  


}

有谁能告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

看起来外部tomcat的CLASSPATH存在问题。确保eclipse项目中的任何库都包含在服务器的类路径或$ {tomcat} / server / lib目录中。

此外,任何.properties,.xml或其他配置也位于类路径中。