Makes sense...

"Craig R. McClanahan" wrote:

> Gabriel Wong wrote:
>
> > Hi,
> >
> > I have some API questions, hopefully someone can shed some light.
> >
> > RequestDistpatcher:
> > Why does forward throw an IllegalStateException while include throw IOException if 
>the
> > ServletOutputStream or a writer had already been obtained from the response object.
> > Shouldn't they both throw the same exception (IllegalStateException)?
> >
>
> The two exceptions are thrown for different reasons.
>
> RequestDispatcher.forward() throws IllegalStateException if you have already grabbed 
>the
> output stream (or writer).  The whole idea of forwarding is that you are delegating
> responsibility for generating this output response to the resource you are 
>forwarding to
> (such as a JSP page).  The purpose of the servlet that performs this call is to do
> whatever business logic is required to store beans in the request, session, or 
>context
> and then forward to the destination resource for output generation.
>
> RequestDispatcher.include() throws IOException (as does forward()) if the destination
> resource throws an I/O error.  I believe that the description of the "throws 
>IOException"
> in the API documentation that was included with the 2.1-EA servlet classes was a 
>botched
> copy-and-paste job in the Javadoc comments.  Note that the include() call does *not*
> throw IllegalStateException, because it is legal for the called resource to generate
> output on the same stream as the calling servlet -- that is the whole point of an 
>include
> operation.
>
> Both of these calls can also throw ServletException -- again, this would be because 
>the
> called resource threw it.
>
> >
> > ServletResponse:
> > Why is it that an exception is not thrown if getWriter/getOutputStream are called 
>more
> > than once?
> >
>
> On the same response, you can call getWriter more than once, or getOutputStream more 
>than
> once.  You just cannot call *both* of them on the same response, because the choice 
>you
> make implicitly selects whether you are generating binary or character output.  If 
>you
> call getOutputStream more than once, you should get a reference to the same stream 
>every
> time.  Correspondingly, calling getWriter more than once should give you back the 
>same
> PrintWriter.
>
> Craig McClanahan
>
> ___________________________________________________________________________
> 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