簡體   English   中英

jfreechart servlet 到 jsp

[英]jfreechart servlet to jsp

我的問題:我有一個jsp,按下按鈕會向servlet 發送一個ID。 有了 iD,我的 Servlet 會將它進一步發送到一個方法,以使用 sql 語句根據 id 從數據庫中獲取數據。 一切正常,甚至圖表。

但現在我想將圖表包含回我的 jsp 並讓用戶在不更改站點的情況下查看它。 對於輸出流,它將保留在 servlet 上,如果我想更改我的數據,我需要返回。 我嘗試使用 saveasPng 路線,但 java 總是說找不到路徑。

任何幫助表示贊賞。

OutputStream outputStream = response.getOutputStream();
        response.setContentType("image/png");
        int width = 500;
        int height = 350;
        ChartUtilities.writeChartAsPNG(outputStream, chart, width, height);
        
//       try {
//           final ChartRenderingInfo info = new 
//            ChartRenderingInfo(new StandardEntityCollection());
//
//            final File file1 = new File(request.getContextPath() + "WebContent/images/piechart.png");
//            ChartUtilities.saveChartAsPNG(
//             file1, chart, 600, 400, info);
//            System.out.println("hallo TRue");
//            } catch (Exception e) {
//            e.printStackTrace();
//            }
         request.setAttribute("imagePath", request.getContextPath() + "/WebContent/images/piechart.png");
         request.setAttribute("chart", chart);
         //request.getRequestDispatcher("Diagramm.jsp").forward(request, response);

我在這里看到兩種不同的方法:

  • 將 JavaScript 添加到您的 JSP 頁面,並使用 AJAX 模式調用您的 servlet 並使用結果在瀏覽器中動態修改 JSP 頁面的內容。
  • 刷新您的 JSP 頁面,添加一個靜態 HTML IMG 標記,您可以在其中使用 IMG 標記的 SRC 屬性內的新 ID 參數調用 servlet。 類似於<IMG src="/MyServlet?ID=xxx" width="500" height="350">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM