tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kaleb Pederson <kpeder...@mail.ewu.edu>
Subject Re: Fresh Response objects: was Subclassing ServletOutputStream/PrintWriter
Date Tue, 03 Aug 2004 19:44:17 GMT
Hello,

On Tuesday 03 August 2004 7:44 am, Shapira, Yoav wrote:
> Your servlet using sendError is fine.  You would then configure an error
> page for this error in your web.xml using the <error-page> directive.
> The server will go to this error page, which can be a servlet, JSP, or
> static page as you wish.  This error page will have a fresh response
> object to which you can write whatever you want.

I wrote a quick little servlet to verify whether indeed I had a "fresh" 
response object or not, and it doesn't appear that I do.  Servlet 1, did the 
following:

doGet(HttpServletRequest request, HttpServletResponse response) {

	ServletOutputStream out = response.getOutputStream();
	sendError(HttpServletResponse.SC_NOT_FOUND);
}

Then, I set the error-page directive so that servlet 2 would receive the 
error.  Servlet 2 then did the following:

doGet(HttpServletRequest request, HttpServletResponse response) {

	PrintWriter out = response.getWriter(); // could call getOutputStream...
	out.println("doh!");
}

When servlet 2 tried to call getOutputStream(), it resulted in a traceback:

ApplicationDispatcher[/kaleb] Servlet.service() for servlet TestServlet1 threw 
exception java.lang.IllegalStateException: getOutputStream() has already been 
called for this response at 
org.apache.coyote.tomcat5.CoyoteResponse.getWriter(CoyoteResponse.java:599) 
at 
org.apache.coyote.tomcat5.CoyoteResponseFacade.getWriter(CoyoteResponseFacade.java:163)
        at TestServlet1.doGet(TestServlet1.java:20)

So, in what case do I really get a fresh response object?  Can a response 
object be considered fresh if you can't write to it?

Thanks again.

--Kaleb

> You don't need to subclass ServletOutputStream or PrintWriter.  In fact
> it's fairly rare that you'd need to extend these for servlet
> applications.
>
> You can accomplish a similar thing to the above declarative error-page
> behavior by using response.sendRedirect in your original servlet that's
> now using sendError.
>
> Yoav Shapira
>
>
>
> This e-mail, including any attachments, is a confidential business
> communication, and may contain information that is confidential,
> proprietary and/or privileged.  This e-mail is intended only for the
> individual(s) to whom it is addressed, and may not be saved, copied,
> printed, disclosed or used by anyone else.  If you are not the(an) intended
> recipient, please immediately delete this e-mail from your computer system
> and notify the sender.  Thank you.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message