Basic servlet question

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
 
B

Bob Rivers

Hi,

It's me again...

Of course I was doing somethig wrong. Now I fixed it.

But, when I call out.flush it appends the information to the old
information, and my progress looks like:

10%
20%
30%
....
100%

I was reading oreilly's java servlet programming again, and they have
an example that uses server push (multipart/x-mixed-replace). But this
feature is not suported by ie.

How do I do a workaround for IE?

TIA,

Bob
 
J

John C. Bollinger

Bob said:
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.
[...]

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.

The "classical" solution is to have the servlet send a status page that
instructs the browser to refresh at intervals; at each refresh the
status message is updated. This makes for a somewhat more complicated
protocol, but it works reliably across browsers.
Another question: can I do this using a servlet listener?

No.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,166
Latest member
DollyBff32
Top