You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Christopher Benson <be...@mindspring.com> on 2001/05/07 19:03:26 UTC

Problem with JSP in Tomcat calling JavaBean that uses Xalan 2

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
benson@mindspring.com
=====================================================
package com.christopherbenson;

import java.io.*;
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());
    }
  }

  private String xmlsource = 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;
  }

  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;


  }

  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;
    }
  }

}
=====================================================

<%@ 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);
%>


=====================================================



Re: Problem with JSP in Tomcat calling JavaBean that uses Xalan 2

Posted by "Noel E. Lecaros" <sp...@mozcom.com>.
Hi, Christopher

Just an idea to get you started.  If I remember correctly, an
IllegalStateException can be thrown if you obtain a Writer to the
ServletOutputStream more than once, e.g. calling Request.getWriter, and then
calling Request.getInputStream.  From your code, I can't tell whether you're
doing so, but I do know that a JSP will try to obtain a Reader from the request
object.  That means any stream- or writer-related accesses in your bean are
suspect.  You can also investigate by looking in the working subdirectory where
Tomcat creates the servlets from JSPs.  The variable names be a bit cryptic, but
this should help you debug your exception dumps.

Hope this helps.

Regards,
Noel Lecaros

> Christopher Benson wrote:
> 
> 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
> benson@mindspring.com
> =====================================================
> package com.christopherbenson;
> 
> import java.io.*;
> 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());
>     }
>   }
> 
>   private String xmlsource = 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;
>   }
> 
>   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;
> 
> 
>   }
> 
>   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;
>     }
>   }
> 
> }
> =====================================================
> 
> <%@ 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);
> %>
> 
> =====================================================
> 
>