gpt4 book ai didi

java - 如何使用 JFreeChart 在外部 Tomcat 中运行报告?

转载 作者:行者123 更新时间:2023-11-28 23:30:22 24 4
gpt4 key购买 nike

我刚刚使用 JFreeChart 创建了报表,它在 Tomcat 中运行,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;
}


}

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

最佳答案

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

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

关于java - 如何使用 JFreeChart 在外部 Tomcat 中运行报告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31128620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com