首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSP XALAN示例

JSP XALAN示例
EN

Stack Overflow用户
提问于 2008-10-24 12:50:59
回答 1查看 576关注 0票数 0

我想做的是以下几点。

将两个参数传递给URL

  • 类型
  • doc_id

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

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

我将如何使用XALAN和JSP页面来完成这个任务?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2008-10-24 13:30:18

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

XALAN站点有一个使用servlet的很好的例子,为了方便,我将在这里复制它:原始的可以找到这里。在本例中,他们对xsl和xml文件的名称进行了硬编码,但这很容易修改以使用您所描述的生成的文件名。重要的是生成的输出被流到浏览器中。

代码语言:javascript
复制
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();
   }  
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/233383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档