【问题标题】:JSP XALAN ExampleJSP XALAN 示例
【发布时间】:2010-09-19 00:04:21
【问题描述】:

我正在尝试做的是以下内容。

将两个参数传递给 URL

  • 类型
  • doc_id

一旦它们通过 URL 传递给 JSP,我想将类型模板应用于 doc_id xml。

因此,如果类型为 001,则 001.xsl 将应用于 doc_id.xml。这个输出我不想存储在文件中,而是直接输出到浏览器。

我将如何使用 XALAN 和 JSP 页面执行此操作?

【问题讨论】:

    标签: jsp xalan


    【解决方案1】:

    我建议将这种类型的代码放在 servlet 而不是 JSP 页面中。如果您有一些需要 JSP 的特定约束,则可以修改代码以在 JSP 页面上工作。

    XALAN 站点有一个使用 servlet 的不错示例,为了方便起见,我将在此处复制该示例: 原文可以在here找到。在此示例中,他们对 xsl 和 xml 文件的名称进行了硬编码,但是很容易修改以使用您所描述的生成的文件名。重要的是生成的输出会流式传输到浏览器。

    public class SampleXSLTServlet extends javax.servlet.http.HttpServlet {
    
        public final static String FS = System.getProperty("file.separator"); 
        // Respond to HTTP GET requests from browsers.
    
        public void doGet (javax.servlet.http.HttpServletRequest request,
                     javax.servlet.http.HttpServletResponse response)
                     throws javax.servlet.ServletException, java.io.IOException
       {
         // Set content type for HTML.
         response.setContentType("text/html; charset=UTF-8");    
         // Output goes to the response PrintWriter.
         java.io.PrintWriter out = response.getWriter();
         try
         {  
            javax.xml.transform.TransformerFactory tFactory = 
               javax.xml.transform.TransformerFactory.newInstance();
            //get the real path for xml and xsl files.
            String ctx = getServletContext().getRealPath("") + FS;        
            // Get the XML input document and the stylesheet, both in the servlet
           // engine document directory.
           javax.xml.transform.Source xmlSource = 
                new javax.xml.transform.stream.StreamSource
                             (new java.net.URL("file", "", ctx+"foo.xml").openStream());
           javax.xml.transform.Source xslSource = 
                new javax.xml.transform.stream.StreamSource
                             (new java.net.URL("file", "", ctx+"foo.xsl").openStream());
           // Generate the transformer.
           javax.xml.transform.Transformer transformer = 
                             tFactory.newTransformer(xslSource);
           // Perform the transformation, sending the output to the response.
          transformer.transform(xmlSource, 
                           new javax.xml.transform.stream.StreamResult(out));
         }
         // If an Exception occurs, return the error to the client.
        catch (Exception e)
        {
           out.write(e.getMessage());
           e.printStackTrace(out);    
        }
        // Close the PrintWriter.
        out.close();
       }  
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-02
      • 2010-09-18
      • 2012-01-02
      • 2013-11-25
      • 2014-08-18
      • 1970-01-01
      • 2015-04-13
      • 2011-07-08
      • 2023-03-23
      相关资源
      最近更新 更多