You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Mike Hogarth <ma...@ucdavis.edu> on 2000/08/11 21:27:12 UTC

behavior with servlets

Hello:

Was wondering whether someone could clarify a certain behavior I see with Xalan when I use it within a servlet. Below is the source code for the servlet.

I actually have 3 servlets, each which invokes the XSLTProcessor with a different xsl file but the same XML file.  The first time I invoke one of them, it takes about 10-15 seconds to respond. After that, I can invoke any of the others and the initial one and it responds immediately. I have tried instantiating the XSLTProcessor object in the servlet's init() method and it doesn't increase the speed while causing an XSLTProcessorException when I hit "reload" on that particular servlet.

Lastly, are there plans to make the XSLTProcessor more 'servlet friendly'?  There doesn't seem to be a way to "trap" the output of the XSLTProcessor to save it for later. I have to give it the output stream when instantiating it and it does the communication with the recipient. With servlets, that is less desirable.

Thanks in advance,
Michael Hogarth,MD

Source Code:
------------------------------------------------------------------

import org.xml.sax.SAXException;
import org.apache.xalan.xslt.XSLTProcessorFactory;
import org.apache.xalan.xslt.XSLTInputSource;
import org.apache.xalan.xslt.XSLTResultTarget;
import org.apache.xalan.xslt.XSLTProcessor;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.net.*;
import java.util.*;

/**
 * Simple sample code to show how to run the XSL processor
 * from the API.
 */
public class about extends HttpServlet
{
 
	InputStream is_xml;
	InputStream is_xsl;
	XSLTProcessor processor;
	BufferedReader r_xml;
	BufferedReader r_xsl;
	
	public void init(ServletConfig config) throws ServletException{
		super.init(config);
	}
	
	public void service(HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{

		ServletOutputStream out = res.getOutputStream();
		res.setContentType("text/html");
		try{
  		  	this.processor = XSLTProcessorFactory.getProcessor();
			ServletContext sc = this.getServletContext();
			this.is_xml = sc.getResourceAsStream("WEB-INF/guidelines/Headache.xml");
			this.is_xsl = sc.getResourceAsStream("WEB-INF/guidelines/about.xsl");
			this.r_xml = new BufferedReader(new InputStreamReader(is_xml));
			this.r_xsl = new BufferedReader(new InputStreamReader(is_xsl));
			processor.process(new XSLTInputSource(r_xml), new XSLTInputSource(r_xsl), new XSLTResultTarget(out));
		
		}
		catch (Exception e){
			out.println(""+e);
		}
	}
}


Re: behavior with servlets

Posted by Cyber Bob <ro...@webcybernetics.com>.
----- Original Message -----
From: Mike Hogarth <ma...@ucdavis.edu>
To: <xa...@xml.apache.org>
Sent: Saturday, August 12, 2000 5:27 AM
Subject: behavior with servlets


> Hello:
>
> Was wondering whether someone could clarify a certain behavior I see with
Xalan when I use it within a servlet. Below is the source code for the
servlet.
>
> I actually have 3 servlets, each which invokes the XSLTProcessor with a
different xsl file but the same XML file.  The first time I invoke one of
them, it takes about 10-15 seconds to respond. After that, I can invoke any
of the others and the initial one and it responds immediately. I have tried
instantiating the XSLTProcessor object in the servlet's init() method and it
doesn't increase the speed while causing an XSLTProcessorException when I
hit "reload" on that particular servlet.
>
Is this latency a symptom of your particular servlet engine and JVM
environment? You may want to include these details as different combinations
have different behavours...

If your concerned with response time you have a few options available. You
can cache a collection of StyleSheetRoot objects which are precompiled. You
can also store a pool of preallocated processor objects. Just remeber to
call reset if you re-use them...

> Lastly, are there plans to make the XSLTProcessor more 'servlet friendly'?
There doesn't seem to be a way to "trap" the output of the XSLTProcessor to
save it for later. I have to give it the output stream when instantiating it
and it does the communication with the recipient. With servlets, that is
less desirable.
>
Actually you have a few onptions here. You could use a ByteArrayOutputStream
for the output. Thus you end up with a bytearray which
automagically grows. You can then convert this to a string etc. This is all
internal to your servlet without using the response output stream.

Hope this helps...