B
Bob Rivers
Hi,
I have a "classical" problem: how to update a html while a servlet is
being processed. I found some answers here, but I was not able to make
it work.
I have a html file that calls a servlet. It's simple as that:
<html>
<head>
</head>
<body>
<table width="100%" border="0">
<tbody align="center">
<tr>
<td><img src="../servlet/GraphicServlet"></td>
</tr>
</tbody>
</table>
</body>
</html>
The servlet uses jFreeChart to build a bar chart as follows:
public class GraphicServlet extends HttpServlet {
public GraphicServlet() {
}
private CategoryDataset createDataset() {
DefaultCategoryDataset data = new DefaultCategoryDataset();
.... JDBC ....
return data;
}
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
OutputStream out = response.getOutputStream();
CategoryDataset data = this.createDataset();
JFreeChart chart = ChartFactory.createStackedBarChart("", "", "",
data, PlotOrientation.VERTICAL, false, true, false);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot cp = (CategoryPlot)chart.getPlot();
cp.setNoDataMessage("Nothing to show");
BarRenderer barrenderer = (BarRenderer)cp.getRenderer();
barrenderer.setDrawBarOutline(false);
barrenderer.setLabelGenerator(new StandardCategoryLabelGenerator("{0}
= {2}", new DecimalFormat("0")));
barrenderer.setItemLabelsVisible(true);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, chart, 700, 500);
}
}
This code works fine. No problem. But, what I am trying to do, is to
keep the users informed while the graph is being processed.
If I try to use something like that:
String msg = "Wait...";
out.write(msg.getBytes());
out.flush();
I receive an error telling that the connection was reset by peer. So I
was not able to write down to the html.
Another question: can I do this using a servlet listener?
Please, help me with some example (both ways) if possible (or tell
where to find it).
TIA,
Bob
I have a "classical" problem: how to update a html while a servlet is
being processed. I found some answers here, but I was not able to make
it work.
I have a html file that calls a servlet. It's simple as that:
<html>
<head>
</head>
<body>
<table width="100%" border="0">
<tbody align="center">
<tr>
<td><img src="../servlet/GraphicServlet"></td>
</tr>
</tbody>
</table>
</body>
</html>
The servlet uses jFreeChart to build a bar chart as follows:
public class GraphicServlet extends HttpServlet {
public GraphicServlet() {
}
private CategoryDataset createDataset() {
DefaultCategoryDataset data = new DefaultCategoryDataset();
.... JDBC ....
return data;
}
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
OutputStream out = response.getOutputStream();
CategoryDataset data = this.createDataset();
JFreeChart chart = ChartFactory.createStackedBarChart("", "", "",
data, PlotOrientation.VERTICAL, false, true, false);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot cp = (CategoryPlot)chart.getPlot();
cp.setNoDataMessage("Nothing to show");
BarRenderer barrenderer = (BarRenderer)cp.getRenderer();
barrenderer.setDrawBarOutline(false);
barrenderer.setLabelGenerator(new StandardCategoryLabelGenerator("{0}
= {2}", new DecimalFormat("0")));
barrenderer.setItemLabelsVisible(true);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, chart, 700, 500);
}
}
This code works fine. No problem. But, what I am trying to do, is to
keep the users informed while the graph is being processed.
If I try to use something like that:
String msg = "Wait...";
out.write(msg.getBytes());
out.flush();
I receive an error telling that the connection was reset by peer. So I
was not able to write down to the html.
Another question: can I do this using a servlet listener?
Please, help me with some example (both ways) if possible (or tell
where to find it).
TIA,
Bob