-1

my problem: i have a jsp where a button press will send a ID to the servlet. with the iD, my Servlet will send it further to a method, to use a sql statement to fetch data from the database base on the id. it all works fine, even the chart.

but now i want to include the chart back to my jsp and let the user see it without changing sites. with the outputstream it will stay on the servlet, and if i want to change my data, i need to go back. i tried going for the saveasPng route, but java always say that path was not found.

any help is appreciated.

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);
thobbe
  • 7
  • 1
  • Are you asking [_How to call servlet through a JSP page_](https://stackoverflow.com/q/5649722/230513)? – trashgod Nov 01 '20 at 14:04
  • @trashgod well my problem is that i want to display the jfreechart on my jsp. right now it is displayed in the servlet, after pressing the button in the jsp. – thobbe Nov 01 '20 at 20:31
  • Why not create a servlet for just the chart, and include it as needed? – trashgod Nov 01 '20 at 21:20

1 Answers1

1

I see two different approaches here:

  • Add JavaScript to your JSP page and using AJAX patterns to invoke your servlet and modify dynamically the content of the JSP page within the browser with the result.
  • Refresh your JSP page adding a static HTML IMG tag where you invoke the servlet with the new ID parameter within the SRC attribute of the IMG tag. Something like <IMG src="/MyServlet?ID=xxx" width="500" height="350">
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jose Miguel Ordax
  • 1,151
  • 1
  • 9
  • 20