嗨,我試圖在jsp上顯示圖像,但失敗了。我生成一個圖表並保存該文件而不是刷新它,現在我想在jsp上顯示這張圖片。我知道這是所有的jsp,我應該使用servlet,但這只是一個原型。在JSP上顯示圖像時出現問題
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.jfree.chart.ChartFactory"%>
<%@ page import="org.jfree.chart.ChartUtilities"%>
<%@ page import="org.jfree.chart.JFreeChart"%>
<%@ page import="org.jfree.chart.plot.PlotOrientation"%>
<%@ page import="org.jfree.data.*"%>
<%@ page import="org.jfree.data.jdbc.JDBCCategoryDataset"%>
<%@ page import="org.jfree.chart.renderer.category.CategoryItemRenderer"%>
<%@ page import="org.jfree.chart.plot.CategoryPlot"%>
<%@ page import="org.jfree.chart.plot.PlotOrientation"%>
<%@ page import="java.awt.Color"%>
<%
String query = "SELECT product_name,price from client";
JDBCCategoryDataset dataset = new JDBCCategoryDataset(
"jdbc:mysql://localhost:3306/client",
"com.mysql.jdbc.Driver", "rootroot", "rootroot");
//"jdbc:mysql://localhost/client", "rootroot", "rootroot"
dataset.executeQuery(query);
JFreeChart chart = ChartFactory.createBarChart3D("Products Vs Price",
"product_name", "price", dataset,
PlotOrientation.VERTICAL, true, true, false);
CategoryPlot plot = chart.getCategoryPlot();
CategoryItemRenderer renderer = plot.getRenderer();
renderer.setSeriesPaint(0, Color.green);
try {
ChartUtilities
.saveChartAsJPEG(
new File(
"C:\\Users\\student\\Documents\\NetBeansProjects\\WebForm\\WebForm-war\\chart.jpg"),
chart, 1000, 1000);
} catch (IOException e) {
System.out.println("Problem in creating chart.");
}
%>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script>
function refreshIMG()
{
$.ajax({
url: "displaychartpricevsprice.jsp", cache: false,
success: function(html){ $("#").html("<img src=\"C:\Users\student\Desktop\WebForm\WebForm-war\\chart.jpg"/>");
callback();
}
});
}
function callback()
{
settimeout("refreshIMG();", 1800000);
//1800= 30seconds in ms
}
refreshIMG();
</script>
</head>
<body>
<div >
<img src="C:\Users\student\Documents\NetBeansProjects\WebForm\WebForm-war\chart.jpg" width=1000 height=1000>
</div>
</body>
我已經嘗試和仍然沒有顯示image –
你的瀏覽器在開發者控制檯中說了些什麼?也許它因爲安全性而被拒絕。 –