Re: HttpServletResponseWrapper error.

2002-01-22 Thread Andreas Junghans

Hi Jay,

 This works with Resin. However, in Tomcat, when I wrap the response, I
 commit the original response object and can not forward to a thank you
 page. I catch a java.lang.IllegalStateException: Cannot forward after
 response has been committed. In my debug tests, the response is committed
 during the RequestDispatchers forward method.

I've had a similar case. The problem is that Tomcat calls flushBuffer() on
the response, which seems to be completely legal regarding the Servlet
spec. Calling flushBuffer() commits the response, and after that, no forward
is possible.

If Resin behaves differently, this is probably due to a different buffer
size or different buffer handling. Perhaps Resin doesn't call flushBuffer()
at all, which (in my understanding) is just as spec compliant as Tomcat's
behaviour.

What you have to do is override flushBuffer() in your response wrapper to
prevent HttpServletResponseWrapper from passing the flush to the original
response. In my case, I simply added an empty flushBuffer() method to the
wrapper, and it worked. However, you should note that some containers
_might_ rely on the side effects of the original flushBuffer() (e.g. that
exceptions are raised when calling methods like sendRedirect() afterwards).
Although this is highly unlikely, you have to simulate these effects in
your wrapper if you want to be _absolutely_ sure your application runs in
every container.

Regards

  Andreas Junghans



--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




HttpServletResponseWrapper error.

2002-01-21 Thread Jay Wright


I am writing to this list after inquiries elsewhere have turned up no
adequate responses.  I hope it is the correct forum for the question, since
I am having trouble on Tomcat whereas I didn't on Resin and need to know how
the Tomcat container handles the HttpServletResponseWrapper.

I have a servlet which needs to write a JSP page's resultant HTML to a file
instead of sending it back to the browser. 

I do this by wrapping the response with HttpServletResponseWrapper, calling
RequestDispatcher and forward on the wrapped response, which runs the JSP
and writes the resultant HTML to a file, then forward control to a
thankyou.html page. 

This works with Resin. However, in Tomcat, when I wrap the response, I
commit the original response object and can not forward to a thank you
page. I catch a java.lang.IllegalStateException: Cannot forward after
response has been committed. In my debug tests, the response is committed
during the RequestDispatchers forward method.

I need to avoid this.  The templates (1 and 2) are all JavaServer Pages
whose output (HTML) is intercepted and written to file by the
MyServletResponse wrapper. How do I do this without committing the
response object. I could find no way of duplicating or cloning the object
before passing it to MyServletResponse's constructor.  Nor could I close the
ServletOutputStreams.  Any ideas?  This worked wonderfully on Resin, but not
Tomcat 4.0.1.  I prefer to use Tomcat. 

If this cannot work, I need to know so I can move on.  If there is a better
way to handle it, I'm open to that as well.  Additionally it is ENTIRELY
reasonable to assume that I have implemented the wrapper incorrectly. 

Thank you,
Jay Wright

THE SERVLET CODE: 

MyServletResponse wrapper1 = new MyServletResponse (response,
1_content.txt); 
MyServletResponse wrapper2 = new MyServletResponse (response,
2_content.txt); 

rd = request.getRequestDispatcher(template1); 
rd.include(request, wrapper1); 
wrapper1.close(); 

rd = request.getRequestDispatcher(template2); 
rd.include(request, wrapper2 ); 
wrapper2 .close(); 

request.getRequestDispatcher(getScreenFlowManager().getTemplate(locale)).for
ward(request, response); 

MY WRAPPER CLASS: 

public class MyServletResponse extends HttpServletResponseWrapper { 

private PrintWriter printWriter; 
private ServletOutputStream servletOutputStream; 
 
/** 
* Constructor 
*/ 
public MyServletResponse (HttpServletResponse response, String filename)
throws java.io.IOException { 
   super(response); 
  servletOutputStream = response.getOutputStream(); 
  File file = new File(filename); 
  String fileCreated = file.toString(); 
   printWriter = new PrintWriter(new BufferedWriter(new
FileWriter(fileCreated))); 
} 

public ServletOutputStream getOutputStream() throws java.io.IOException
{ 
   return servletOutputStream; 
} 

public void setOutputStream(ServletOutputStream sos) throws
java.io.IOException { 
servletOutputStream = sos; 
} 

public PrintWriter getWriter() throws java.io.IOException { 
return printWriter; 
} 

public void close() { 
this.printWriter.close(); 
} 
} 

--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]