I struggled with the same basic problem in my servlets before JSPs were
available. The best solution for me ended up being using servlet code for
doing all of my database access & business logic. Only after having all the
data I need to build my page saved in my httpsession object I then use the
sendRedirect() or RequestDispatcher.forward() to give control to a JSP page
that builds the page with the data that the servlet retrieved or else
another JSP page that can display a readable error message. This puts a
minimum of java code in the JSP page and little or no chance of an exception
being thrown there. If need be, the JSP page can contain code that cleans up
the junk you passed it via the httpsession object.

Using this design approach will also put you in a better position if you
later decide to give your application a face lift by removing the html forms
and replacing them with some other front end GUI such as Applets.

John Zerbe - Mellon Bank
Phone:  412-234-1048   E-Mail: [EMAIL PROTECTED]



> -----Original Message-----
> From: Srikanth Ranganathan [SMTP:[EMAIL PROTECTED]]
> Sent: Thursday, April 29, 1999 3:46 PM
> To:   [EMAIL PROTECTED]
> Subject:      Re: An alternative to sendRedirect()
> orRequestDispatcher.forward()
>
>  The core of my problem is as follows:
> I have bracketed all my JSP's with a try catch block. If I encounter any
> exception while
> processing a JSP page, then I redirect to a different page with the error
> message from
> the exception trace.
>
> However, I dont want to perform a sendRedirect with a get operation,
> because
> the url
> is lengthy and ugly. Instead I would love to do a post, get the response
> from
> the url connection,
> remove the contents of the current print writer and replace it with that
> of the
> response
> from the url connection.
>
> I don't know how to dis-associate the print writer from the response
> object
> which has some
> partial content, generated by the servlet.
>
> -Srikanth
>
> [EMAIL PROTECTED] wrote:
>
> > 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
>
> __________________________________________________________________________
> _
> 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