//Import the necessary classes import java.io.*; import java.util.Enumeration; import javax.servlet.*; import javax.servlet.http.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; public class ProcessXSL extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String xmlURL = request.getParameter("xml"); String xslURL = request.getParameter("xsl"); PrintWriter out = response.getWriter(); response.setContentType("text/html"); try { // Create transformer factory TransformerFactory factory = TransformerFactory.newInstance(); // Use the factory to create a template containing the xsl file Templates template = factory.newTemplates(new StreamSource(xslURL)); // Use the template to create a transformer Transformer xformer = template.newTransformer(); //Set xsl params Enumeration enum = request.getParameterNames(); while (enum.hasMoreElements()) { String pname = (String) enum.nextElement(); if (!(pname.equals("xml") || pname.equals("xsl"))) { String pvalue = request.getParameter(pname); xformer.setParameter(pname,pvalue); } } // Prepare the input and output files Source source = new StreamSource(xmlURL); Result result = new StreamResult(out); // Apply the xsl file to the source file and write the result to the output file xformer.transform(source, result); } catch (Exception e) { out.println(e.getMessage()); } }//End of doPost }//End of class Definition