How about using another writer to capture the response data until you know
that no error occured.  If an error occurs, then throw out the captured
response data and call sendError.  If no error occurs, then transfer the
complete response data to the response object.

-Spike

------Sample code --------

StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);

try{
   //Capture response data by writing to the StringWriter
   generateReponse(writer)

  //no error occurred, so transfer the completed response data to the
response writer.
   writer.close();
   response.getWriter().println(stringWriter.toString());
}
catch(Throwable th){
   //error occurred - all data written to the stringWriter will be ignored.
  response.sendError(response.SC_INTERNAL_SERVER_ERROR, th.getMessage());
}


-----
Spike Washburn
IBM WebSphere Application Server
Internet E-mail: [EMAIL PROTECTED]



Srikanth Ranganathan <[EMAIL PROTECTED]> on 04/29/99 12:14:58 PM

Please respond to "A mailing list for discussion about Sun Microsystem's
      Java              Servlet API Technology."
      <[EMAIL PROTECTED]>

To:   [EMAIL PROTECTED]
cc:    (bcc: Donald Washburn/Raleigh/IBM)
Subject:  Re: An alternative to sendRedirect()
      orRequestDispatcher.forward()





I am struggling with the same problem. I would like to clean out the
print writer buffer, and substitute it with the contents of the error
message.

Does anybody have ideas as to deal with exceptions / error messages, when a
partial
response has been written into the printwriter out stream ?


-Srikanth

Neal Kaiser wrote:

> I've realized one problem (and am still looking for a good solution). Let
> me explain how a sample JSP flows:
>
> <Database Query is Done>
> Things printed to PrintWriter
> <Another Database Query is Done>
> This printed
> ...etc
>
> Now my <Database Query Done> tags can do a sendRedirect if there is an
> error executing the SQL.
>
> So, say my first database query is done, then things are printed, then
the
> second query bombs. It will try to do a sendRedirect, which ofcourse
won't
> work because the servlet output stream has been written to. (Aside note,
> this DID work with build 131 - but according to the spec, it probably
> shouldn't have).
>
> As I see it, my options are to either:
>
> 1) Do all queries at the top of the JSP before any HTML is printed. The
way
> the JSPs are structured, this would not really be acceptable.
>
> 2) Somehow have my "out" PrintWriter really be a buffer. Then, when
> everthing is done, print to the real output stream.  I'm not sure how
> possible this would be....
>
> Any other ideas?
>
> Thanks.
>
> At 09:25 AM 4/29/99 -0400, you wrote:
> >I am using the latest JRun build (140) and much of my JSP functionality
is
> >failing.  In many of my JSPs, I do some preliminary processing of a
> >request, and if certan conditions are met, I forward the request to
another
> >JSP for processing.
> >
> >I do all this BEFORE anything is written to the PrintWriter (Servlet
> >OutputSteam).  This previously worked w/ build 131, but now doesn't.
I've
> >tried the RequestDispatcher.forward() also, no luck. In reading the
spec,
> >it says "You cannot use this method if the PrintWriter object has been
> >obtained from the response..."
> >
> >Well, when JRun compiles a JSP to a JAVA file, one fo the first lines in
> >the service() method is:
> >
> >PrintWriter out = new PrintWriter (new BufferedWriter( new
> >OutputStreamWriter(Response.getOutputStream())),true)
> >
> >How can I get around this? I want to be able to forward requests, but
> >cannot because getOutputStream has been called.
> >
> >Thanks,
> >
> >Neal Kaiser
> >
> >------------------------------------------------------------
> >To unsubscribe, send email to [EMAIL PROTECTED]
> >and include in the body of the message "unsubscribe jrun-development".
> >
> >
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to