OK, here is the problem I have. I have a servlet that dynamically creates an image (not based on parameters) which I do NOT want to save on the HDD.
I also have a HTML template (JSP) where I want to show that picture.
I create my image as following:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PieDataset dataset = createDataset();
JFreeChart chart = createChart(dataset, "OS Usage");
resp.setContentType("image/png");
ChartUtilities.writeChartAsPNG(resp.getOutputStream(), chart, 500, 400);
req.getRequestDispatcher("report.jsp").forward(req, resp);
}
My JSP looks like this:
<body>
<h1>OS usage</h1>
<p>Here are the results of OS usage in survey that we completed.<br>
<p><img alt="OS usage" src="/reportImage">
</body>
When that code is executed, I get what I expected, from the way I wrote my code, a image only, without any html.
My question is how to do it so the HTML renders as well.
Thx.
You should have two servlets:
<img src="/reportImage" .../>. When the browser sees this tag, it sends a second request, to the URL/reportImage./reportImageURL. This servlet should only send the bytes of the image to the response output stream. It must not forward to a JSP.