KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servlet > SimpleXSLTServlet


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: SimpleXSLTServlet.java,v 1.7 2004/02/17 19:13:22 minchau Exp $
18  */

19 package servlet;
20
21 import javax.servlet.*;
22 import javax.servlet.http.*;
23 import java.io.*;
24 import java.net.URL JavaDoc;
25
26 import javax.xml.transform.TransformerFactory JavaDoc;
27 import javax.xml.transform.Transformer JavaDoc;
28 import javax.xml.transform.Source JavaDoc;
29 import javax.xml.transform.stream.StreamSource JavaDoc;
30 import javax.xml.transform.stream.StreamResult JavaDoc;
31
32 /*
33  * This sample applies the todo.xsl stylesheet to the
34  * todo.xml XML document, and returns the transformation
35  * output (HTML) to the client browser.
36  *
37  * IMPORTANT: For this to work, you must place todo.xsl and todo.xml
38  * in the servlet root directory for documents.
39  *
40  */

41
42 public class SimpleXSLTServlet extends HttpServlet {
43
44   /**
45    * String representing the file separator characters for the System.
46    */

47   public final static String JavaDoc FS = System.getProperty("file.separator");
48   
49   public void init(ServletConfig config) throws ServletException
50   {
51     super.init(config);
52   }
53
54   public void doGet (HttpServletRequest request,
55                      HttpServletResponse response)
56     throws ServletException, IOException, java.net.MalformedURLException JavaDoc
57   {
58     // The servlet returns HTML.
59
response.setContentType("text/html; charset=UTF-8");
60     // Output goes in the response stream.
61
PrintWriter out = response.getWriter();
62     try
63     {
64       TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
65       //get the real path for xml and xsl files.
66
String JavaDoc ctx = getServletContext().getRealPath("") + FS;
67       // Get the XML input document and the stylesheet.
68
Source JavaDoc xmlSource = new StreamSource JavaDoc(new URL JavaDoc("file", "", ctx+"birds.xml").openStream());
69       Source JavaDoc xslSource = new StreamSource JavaDoc(new URL JavaDoc("file", "", ctx+"birds.xsl").openStream());
70       // Generate the transformer.
71
Transformer JavaDoc transformer = tFactory.newTransformer(xslSource);
72       // Perform the transformation, sending the output to the response.
73
transformer.transform(xmlSource, new StreamResult JavaDoc(out));
74     }
75     catch (Exception JavaDoc e)
76     {
77       out.write(e.getMessage());
78       e.printStackTrace(out);
79     }
80     out.close();
81   }
82   
83 }
84
Popular Tags