J
John English
I have an application where the servlets generate XML and their output
goes through an XSLT filter. This all works fine, except when I try to
use RequestDispatcher's include() method to paste in some external
content. Whenever I try to do this, the XS:T filter bombs out with
an IllegalStateException at the point where it calls getWriter() to
get the output stream. The code looks something like this:
In the servlet, inside doGet(), I have:
println("<html>");
try {
RequestDispatcher disp =
context.getRequestDispatcher("/foo.html");
disp.include(request,response);
}
catch (Exception e) {
System.out.println("Include failed: " + e.getMessage());
// this never happens... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
e.printStackTrace();
}
println("</html>");
(using <html> to delimit HTML content in the enclosing XML).
In the XSLT filter I have:
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws ServletException, IOException {
try {
CharResponseWrapper wrapper =
new CharResponseWrapper((HttpServletResponse)response);
chain.doFilter(request,wrapper);
String s = wrapper.toString();
if (s.length() > 0) {
InputSource input = new InputSource(new StringReader(s));
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAXParser parser = spf.newSAXParser();
reader = parser.getXMLReader();
stf = (SAXTransformerFactory) TransformerFactory.newInstance();
filter = stf.newXMLFilter(new StreamSource(stylesheet));
filter.setParent(reader);
StreamResult result = new StreamResult(response.getWriter());
// fails here... ^^^^^^^^^^^^^^^^^^^^
// but only when I've used include() in the servelt being filtered!
Transformer transformer = stf.newTransformer();
SAXSource transformSource = new SAXSource(filter, input);
transformer.transform(transformSource, result);
(... and so on...)
Does anyone have any idea why the response writer might end up in
an illegal state because of using include()?
goes through an XSLT filter. This all works fine, except when I try to
use RequestDispatcher's include() method to paste in some external
content. Whenever I try to do this, the XS:T filter bombs out with
an IllegalStateException at the point where it calls getWriter() to
get the output stream. The code looks something like this:
In the servlet, inside doGet(), I have:
println("<html>");
try {
RequestDispatcher disp =
context.getRequestDispatcher("/foo.html");
disp.include(request,response);
}
catch (Exception e) {
System.out.println("Include failed: " + e.getMessage());
// this never happens... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
e.printStackTrace();
}
println("</html>");
(using <html> to delimit HTML content in the enclosing XML).
In the XSLT filter I have:
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws ServletException, IOException {
try {
CharResponseWrapper wrapper =
new CharResponseWrapper((HttpServletResponse)response);
chain.doFilter(request,wrapper);
String s = wrapper.toString();
if (s.length() > 0) {
InputSource input = new InputSource(new StringReader(s));
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAXParser parser = spf.newSAXParser();
reader = parser.getXMLReader();
stf = (SAXTransformerFactory) TransformerFactory.newInstance();
filter = stf.newXMLFilter(new StreamSource(stylesheet));
filter.setParent(reader);
StreamResult result = new StreamResult(response.getWriter());
// fails here... ^^^^^^^^^^^^^^^^^^^^
// but only when I've used include() in the servelt being filtered!
Transformer transformer = stf.newTransformer();
SAXSource transformSource = new SAXSource(filter, input);
transformer.transform(transformSource, result);
(... and so on...)
Does anyone have any idea why the response writer might end up in
an illegal state because of using include()?