I personally prefer to keep all database access out of JSP pages -- in fact,
I prefer to minimize the amount of Java in JSP pages as much as possible.  I
believe our approach would solve your redirect problem:

We do all database access and most Java logic in a servlet we call a
JSPPageLoadHandler.  Each JSP page that requires set-up with data from the
database has a corresponding load handler.  This servlet fetches the data
from the database and checks for error conditions.  If it encounters an
error, it puts an error message object into the session and redirects to the
appropriate JSP page, which takes the error message object out of the
session and displays it.  If the queries were successful, the data is placed
into the session, and the servlet redirects to the JSP page.  This JSP page
gets the data out of the session and displays it.  Similarly, if a JSP page
is a form, the form submit calls its JSPPageProcessHandler (servlet) which
reads the data from the form and validates it.  If the data is valid, it
writes it to the database, writes a success message to the session, and
redirects to a page that displays the success message.  If the validation
fails, it sticks an error message into the session and redirects to the
original JSP page, which displays the error message as well as redisplays
the form.

What we like about this approach is that almost all of our Java is in a
servlet, which never writes to the output stream.  Our web staff can make
the JSP pages as pretty as they'd like without worrying about stepping all
over our Java, and our Java code is mostly in servlets and entirely free of
HTML.

        -- Kyal
----------------------------------------------------
Kyallee Dalrymple           [EMAIL PROTECTED]
Software Engineer           (303)-277-1997 x108
Polygon Network, Inc.       http://www.polygon.net/
----------------------------------------------------

-----Original Message-----
From: A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]On Behalf Of Neal
Kaiser
Sent: Thursday, April 29, 1999 9:56 AM
To: [EMAIL PROTECTED]
Subject: Re: An alternative to sendRedirect() or
RequestDispatcher.forward()


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

Reply via email to