2015-11-19 36 views
2

嗨,我試圖在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> 

回答

0

你必須把file://(或嘗試 - file:///)在圖片來源網址的開頭。

+0

我已經嘗試仍然沒有顯示image –

+0

你的瀏覽器在開發者控制檯中說了些什麼?也許它因爲安全性而被拒絕。 –

0

語法沒有任何問題。您正在生成該圖像並嘗試在同一頁上顯示它。

我可以看到的唯一問題是,頁面加載時圖像不可用。嘗試在頁面加載之前創建該圖像,或者如果要在加載頁面後顯示該圖像,請使用ajax。

0

存儲在您的Web應用程序目錄中的圖像,並請使用相對URL的圖片標記

<img src="<folder>/chart.jpg"/> 

,或者你可以使用EL表達式

<img src="${pageContext.request.contextPath}/<folder>/chart.jpg">