[jdom-interest] JDOM via Servlet
Szegedi, Attila
szegedi at scriptum.hu
Wed Jul 25 01:23:20 PDT 2001
Hint: instead of manually caching the POSTed content, try first writing the
content to the URLConnection's OutputStream, and just AFTER that call
connect(). It sounds wild, but it works (I've tested this with many VMs). It
will force URLConnection into using its own buffering of content - and
probably automatically set the Content-Length as well (on a sidenote this is
the only way that works when used in an applet in Netscape Navigator 4.x -
NN mandates writing the output stream first, then connecting. Believe me, I
learned this the hard way.)
Attila.
> -----Original Message-----
> From: jdom-interest-admin at jdom.org
> [mailto:jdom-interest-admin at jdom.org]On Behalf Of Jon Baer
> Sent: Tuesday, July 24, 2001 12:27 PM
> To: jdom-interest at jdom.org
> Subject: Re: [jdom-interest] JDOM via Servlet
>
>
> > > 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.
>
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/yo
uraddr at yourhost.com
More information about the jdom-interest
mailing list