CSci 4608 Principles of Web Programming, UMM: XSL processing in a servlet.

The path to the xml and xsl files is relative to jakarta-tomcat-5.0.30/bin. At this point the files are right in this directory. In general, this is not a good idea, creating a subfolder would be better.




import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.transform.stream.StreamSource; 
import javax.xml.transform.stream.StreamResult; 

public class ExampleServlet extends HttpServlet {


    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException	       
    {
       
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

	String XMLSource = "food.xml";
	String XSLStylesheet = "food.xsl";
	
	try {
	
	// Instantiate a TransformerFactory.
	javax.xml.transform.TransformerFactory tFactory = 
                  javax.xml.transform.TransformerFactory.newInstance();

	// Use the TransformerFactory to process the stylesheet Source and
	// generate a Transformer.
	javax.xml.transform.Transformer transformer = tFactory.newTransformer
                (new javax.xml.transform.stream.StreamSource(XSLStylesheet));

	// Use the Transformer to transform an XML Source and send the
	// output to a Result object.
	transformer.transform
	    (new javax.xml.transform.stream.StreamSource(XMLSource), 
	     new javax.xml.transform.stream.StreamResult(out));

	} catch (javax.xml.transform.TransformerException e) {
	    System.out.println("Transformer exception " + e);
	}
	
    }
}

CSci 2101 home page

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.