[jdom-interest] JDOM via Servlet
Jon Baer
jonbaer at digitalanywhere.com
Tue Jul 24 03:27:18 PDT 2001
> > I seem to continue to be running up against a brick wall trying to pass XML
> > to a servlet and then building a JDOM Document (see SAXBuilder.buid()
> > weirdness and second try). I have decided to ignore my entire back-end
I think I found your problem maybe(?) I was browsing Java site for some bugs and took a
look @ the Top 25 Bugs and it lists it here:
http://developer.java.sun.com/developer/bugParade/bugs/4186170.html
POST to servlet fails - connection reset by peer
Seems like alot of people had the same problem, there was one suggestion which I copied
below.
- Jon
The problem can be fixed by setting the EXACT response
length with response.setContentLength(), prior
to writing
anything to the printWriter.
As this length is not usually known in advance,
the
following workaround can be used instead:
// Print in memory using a
PrintWriter->StringWriter
chain
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
// Generate the HTML...
your_methods_here(pw);
// Get the memory image of the generated
text
StringBuffer buffered_response =
sw.getBuffer();
// Say we return HTML text
response.setContentType("text/html");
// The more important thing: give the exact
response
length
response.setContentLength(buffered_response.length());
// Then write all at once
ServletOutputStream os =
response.getOutputStream();
os.print(buffered_response.toString());
os.close();
// Note: This is not memory-efficient since
os.print
(String) will
// in turn make a second copy of the
buffered
bytes.
More information about the jdom-interest
mailing list