XSL Servlet

The Java servlet shown, below, is a rewrite of the Java XSL application, discussed in a preceding section.  Changes are minimal, most of which are concerned with the servlet vs. application java context.

One bit of hard-won knowledge concerns the parser/transformation classes and engine used by the particular Java server on which the servlet runs.  It was deployed to an Apache Tomcat server.  Tomcat includes an early version of the Java XML packages and a transformation engine.  However, there is an incompatibility between those resources and current versions that shows up in incompatible namespaces.  To ensure that the XML and XSL files would produce the same results from both the application and the servlet, the same Jar files used for the application were provided to the tomcat server.  This was attempted first by setting the system and/or user CLASSPATHs, but to no avail.  The appropriate JAR files , in this case xalan.jar and xerces.jar, were next placed in the tomcat lib directory.  I thought this would solve the problem, since tomcat constructs its own CLASSPATH when it loads and would include them.  But this did not fix the problem, either.  It turns out that when tomcat builds the CLASSPATH, it does so from each JAR file contained within its lib directory, but it does so in alphabetical order by file name.  Thus, xalan and xerces appeared late in the list.  The "fix" was to change the xerces file name to axerxes so that it would be encountered before the classes in default JAR file that appears later in the CLASSPATH.

To run the servlet, you need to include the names of the XML and XSL files as extensions to the URL.

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;
import javax.xml.transform.stream.*; 

import java.io.*; 
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class jbsXMLServlet extends HttpServlet {

	public void init(ServletConfig config) throws ServletException
  	{
    		super.init(config);
  	}

  	public void doGet (HttpServletRequest request,HttpServletResponse response)
    		throws ServletException, IOException
  	{

		String[] pxml = request.getParameterValues("xml"); 
		String[] pxsl = request.getParameterValues("xsl"); 
		String xmlFile = pxml [0];
		String xslFile = pxsl [0];

		PrintWriter out = new PrintWriter ( response.getOutputStream() );
		Source xmlSource = new StreamSource(new URL("file:" + xmlFile).openStream() );
		Source xslSource = new StreamSource(new URL("file:" + xslFile).openStream() );
		response.setContentType ( "text/html" );

		try {
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer(new StreamSource(xslFile));
			transformer.transform(new StreamSource(xmlFile), new StreamResult(out));
	
		}  catch ( Exception e )  {
			out.write ( e.getMessage() );
			e.printStackTrace( out );
		}

  	}  // end doGet
}

Run the servlet:
 (http://jbspc-cs.cs.unc.edu:8888/servlet/jbsXMLServlet?xml=jbsAddress.xml&xsl=jbsAddress.xsl)