Yan,

Thanks - that fixed it. There are a couple of subtle points worth noting:

1. From JSP it is impossible not to write anything to the PrintWriter after
the redirect call because if the last characters of a JSP page are %> the
page will not compile. i.e. There must be at least a CR/LF or space after
the Java script.

2. This can be overcome by returning immediately after the redirect but, as
you've shown, if the return call is not in a logical branch then the page
will not compile because of unreachable code.

Regards,
        Mike

> ----------
> From:         Yan Pujante[SMTP:[EMAIL PROTECTED]]
> Sent:         22 February 1999 16:49
> To:   [EMAIL PROTECTED]
> Subject:      Re: response.sendRedirect(<url>) raises java.io.Exception:
> triedto write more than content length
>
> Rolland Crunk wrote:
> >
> > At 01:43 PM 2/19/99 +0000, Westaway Mike M wrote:
> > >I'm running JSP 0.92 on sun sparc.
> > >
> > >any ideas why ?
> > >
> >
> > Using the JavaWebServer or the servlet runner
> (sun.servlet.http.HttpServer), right?
> > I too have encountered this problem as well, but have no explaination as
> to its
> > cause.  It seems to occur more frequently with the servlet runner, and I
> haven't
> > seen it happen if response.sendRedirect() is called from within a
> servlet (as
> > opposed to a jsp).  I haven't encountered it with IBM WebSphere/Apache
> and
> > haven't tried any of the other jsp engines.
> >
> > So in response to your question, no ideas why, but if you or anyone else
> solves
> > this, please post to the list (as I will do if I stumble across the
> answer).
> >
> > rc
> >
>
> I too encountered the problem on several occasions. I am not sure this
> is the real explanation of the problem, but here is what I have noticed.
> When you use response.sendRedirect(), there shouldn't be any "write" or
> "print" to the output stream (or writer), before or after this call. I
> think that this call is not expecting anything to come with it because
> you are specifying the address of the new page to fetch. Thus if you
> write to the output stream, depending on the implementation, it will
> notice that you are writing more than content length, because it is
> supposed to be 0...
>
> Thus when using sendRedirect(), you should be carefull and write
> somenthing like:
>
> service()
> {
>   // code that DO NOT write on output stream
>
>   if(my_condition_is_true)
>   {
>      response.sendRedirect(..);
>      return; // so that you are sure not to write something to the
> output stream
>   }
> }
>
> If you have a jsp page like that:
> <HTML>
> <BODY>
> <%
>   if(my_condition_is_true)
>   {
>     response.sendRedirect(...);
>     return; // for the same reason!!!
>   }
> %>
> </BODY>
> </HTML>
>
> Obviously this will raise the exception because <HTML><BODY> will be
> sent to the output stream before the redirection... I agree that it is
> not really easy to manage!
>
> Hope this helps.
>
> Yan
>
>

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to