| Laurent P. 2004-03-25, 7:39 am |
| Hello,
I'm migrating a web site from VAJ3.5/WAS3.5 to WSAD/WAS5.
I experienced trouble with the ChainedResponse in WAS5 during
ChainedResponse.transferResponse().
I get a java.lang.ClassCastException:
com.ibm.ws.webcontainer.srt.SRTServletResponse.
A jsp JSP_1 include a servlet SERVLET_1 witch include a jsp JSP_2.
SERVLET_1 create a ChainedResponse passed to JSP_2.
The aim is to work on an different response to detect pb, exception
before sending back the content and instead sending an error message.
--- code example SERVLET_1---
ChainedResponse storedResponse = new
ChainedResponse(request,response);
try {
RequestDispatcher dispatcher =
request.getRequestDispatcher("/MyFinalJSP.jsp");
dispatcher.include(request, storedResponse);
} catch(Throwable t) {
try {
PrintWriter responseWriter = response.getWriter();
responseWriter.println("ERROR "+t.toString());
responseWriter.flush();
}
catch(IOException e) { //}
return;
}
storedResponse.transferResponse(response); // => PB HERE
....
-----------------------
In the WAS3.5 ChainedResponse, you can do getWriter() and
getOutputStream() even if it wasn't recommended but there was no
exception throwed.
In WAS5 ChainedResponse, you can only do getWriter(), or
getOutputStream(), as specified in the J2EE API. If you call one of
these two methods, when you can the second one, it throws an
IllegalStateException. I'm ok on it.
Here is a part of the new WAS 5 code of
ChainedResponse.transfer(HttpServletResponse
httpservletresponse) :
---------
....
ServletOutputStream servletoutputstream;
try {
servletoutputstream = httpservletresponse.getOutputStream();
} catch(IllegalStateException illegalstateexception) {
for(; httpservletresponse instanceof
HttpServletResponseWrapper; httpservletresponse =
(HttpServletResponse)((HttpServletRespon
seWrapper)httpservletresponse).getResponse());
for(; httpservletresponse instanceof IncludedResponse;
httpservletresponse =
((IncludedResponse)httpservletresponse).getProxiedHttpServletResponse());
StoredResponse storedresponse = (StoredResponse)httpservletresponse;
servletoutputstream = storedresponse._out;
}
servletoutputstream.write(getOutputBuffer());
....
---------
My explanation :
At the beginning of the SERVLET_1, JSP_1 had already written in the
"out" (so had already got the writer by getWriter()) of the response.
Doing "servletoutputstream = httpservletresponse.getOutputStream();"
throws an IllegalStateException
as expected by the API. So we go in the catch.
The "httpservletresponse" argument is the initial response so it's a
com.ibm.ws.webcontainer.srt.SRTServletResponse witch implements :
- HttpServletResponse
- IOutputStreamObserver
- IResponseOutput
- IExtendedResponse
SRTServletResponse isn't an instance of HttpServletResponseWrapper or
IncludedResponse, so the 2 for
instructions aren't done.
>StoredResponse storedresponse = (StoredResponse)httpservletresponse;
=> here is the cast exception but I don't understand this cast. We
aren't supposed to have a
StoredResponse here.
I also tried :
One possible solution was to bufferize JSP_1.
When jsp is buffered, the jsp doesn't work with the Writer while
writing whith out.print(). It gets
the Writer at the end of the _jspService(). So in my case, the
transferResponse() was good but at the
end of the jsp, there is a JspWriterImpl.fushBuffer() witch calls
getWriter() => throws an
IllegalStateException because the transferResponse() did an
getOutputStream() before :-(.
Is there any restriction in WAS5 API with ChainedResponse?
It's a bug? Did we misused ChainedResponse?
Any help is welcome.
Thank's for advance.
Laurent.
|