I created a bean
that can be called from a JSP or by directly instantiating it
from main(). The bean uses an XML file and an XSLT file, both of
which are valid. When I run the bean using the main() method, it works
correctly and prints the correct HTML output to the console. However, when
I call the bean from the JSP below in Tomcat, it gives me the following
error:
java.lang.IllegalStateException: Response has already been
committed
I already know that
an IllegalStateException "signals that a method has been invoked at an
illegal or inappropriate time. In other words, the Java environment or Java
application is not in an appropriate state for the requested
operation."
I don't know how to
solve it though. Any ideas?
Thanks,
Christopher
Benson
=====================================================
package
com.christopherbenson;
import
java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
public class
TransformationBean
{
{
public
TransformationBean(){}
public static
void main(String[] args)
throws Exception
{
TransformationBean bean = new TransformationBean();
try
{
bean.setXMLdoc("C:/Temp/test.xml");
bean.setXSLTdoc("C:/Temp/test.xsl");
String x = bean.getResult();
System.out.println(x);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
throws Exception
{
TransformationBean bean = new TransformationBean();
try
{
bean.setXMLdoc("C:/Temp/test.xml");
bean.setXSLTdoc("C:/Temp/test.xsl");
String x = bean.getResult();
System.out.println(x);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
private
String xmlsource = null;
private String xsltsource = null;
private String xsltsource = null;
public void
setXMLdoc(String xmldoc)
throws FileNotFoundException, IOException
{
BufferedReader in = new BufferedReader(new FileReader(xmldoc));
String s, s2 = new String();
while((s = in.readLine())!= null)
s2 += s + "\n";
in.close();
xmlsource = s2;
}
throws FileNotFoundException, IOException
{
BufferedReader in = new BufferedReader(new FileReader(xmldoc));
String s, s2 = new String();
while((s = in.readLine())!= null)
s2 += s + "\n";
in.close();
xmlsource = s2;
}
public void
setXSLTdoc(String xsltdoc)
throws FileNotFoundException, IOException
{
BufferedReader in = new BufferedReader(new FileReader(xsltdoc));
String t, t2 = new String();
while((t = in.readLine())!= null)
t2 += t + "\n";
in.close();
xsltsource = t2;
throws FileNotFoundException, IOException
{
BufferedReader in = new BufferedReader(new FileReader(xsltdoc));
String t, t2 = new String();
while((t = in.readLine())!= null)
t2 += t + "\n";
in.close();
xsltsource = t2;
}
public String getResult()
throws TransformerException, TransformerFactoryConfigurationError,
TransformerConfigurationException, IOException //SAXException
{
try
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(new StringReader(xsltsource)));
StringWriter resultWriter = new StringWriter();
StreamResult TheResult = new StreamResult(resultWriter);
transformer.transform(new StreamSource(new StringReader(xmlsource)), TheResult);
String result = resultWriter.toString();
return result;
}
catch (Exception e)
{
String result = e.getMessage();
return result;
}
}
throws TransformerException, TransformerFactoryConfigurationError,
TransformerConfigurationException, IOException //SAXException
{
try
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(new StringReader(xsltsource)));
StringWriter resultWriter = new StringWriter();
StreamResult TheResult = new StreamResult(resultWriter);
transformer.transform(new StreamSource(new StringReader(xmlsource)), TheResult);
String result = resultWriter.toString();
return result;
}
catch (Exception e)
{
String result = e.getMessage();
return result;
}
}
}
=====================================================
=====================================================
<%@ page language="java"
%>
<jsp:useBean id="bean" class="com.christopherbenson.TransformationBean" />
<%
bean.setXMLdoc("C:/Temp/test.xml");
bean.setXSLTdoc("C:/Temp/test.xsl");
String result = bean.getResult();
out.println(result);
%>
<jsp:useBean id="bean" class="com.christopherbenson.TransformationBean" />
<%
bean.setXMLdoc("C:/Temp/test.xml");
bean.setXSLTdoc("C:/Temp/test.xsl");
String result = bean.getResult();
out.println(result);
%>
=====================================================