Java 为什么我的web应用程序在tomcat/bin中查找所需文件,而不是在web应用程序中?

Java 为什么我的web应用程序在tomcat/bin中查找所需文件,而不是在web应用程序中?,java,tomcat,servlets,tomcat6,Java,Tomcat,Servlets,Tomcat6,我正在做一把叉子。在oryxRoot\editor\server\src\org\oryxeditor\server中,我添加了一个Java servlet,尝试将XSL文件应用于表示XML文档的流。代码如下: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //get AppML from POST

我正在做一把叉子。在
oryxRoot\editor\server\src\org\oryxeditor\server
中,我添加了一个Java servlet,尝试将XSL文件应用于表示XML文档的流。代码如下:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  //get AppML from POST
  InputStream inputStream = new ByteArrayInputStream(request.getParameter("content").getBytes("UTF-8"));
  inputStream.reset();
  try {  
      TransformerFactory tFactory = TransformerFactory.newInstance();
      Transformer transformer = tFactory.newTransformer(new StreamSource("lib/SorinCode.xsl"));
      transformer.transform(new StreamSource(inputStream), new StreamResult(new FileOutputStream("output.txt")));
      System.out.println("************* The result is in output.txt *************");
  } catch (Throwable t) {
      t.printStackTrace();
  }
}
问题是,
newstreamsource(“lib/SorinCode.xsl”)
希望在
tomcat/bin/lib
中找到xsl文件,而不是像我所期望的那样在
tomcat/webapp/oryx/lib
中。我试图改变
oryxRoot/editor/etc/context.xml
并添加
baseDoc=“oryx”
,但这没有帮助

有人能告诉我为什么应用程序要在
bin
文件夹中查找XSL文件,以及我应该如何使其在
webapps/oryx
中查找吗?

cwd(当前工作目录)通常基于程序启动的路径。也就是说,Tomcat程序,而不是您的Web应用程序。cwd可能相当不可预测。它可能只是bin目录,因为您启动Tomcat时shell就在该目录中

要获取部署web应用程序的路径,请使用
request.getServletContext().getRealPath(“/”)

cwd(当前工作目录)通常基于启动程序的路径。也就是说,Tomcat程序,而不是您的Web应用程序。cwd可能相当不可预测。它可能只是bin目录,因为您启动Tomcat时shell就在该目录中


要获取部署web应用的路径,请使用
request.getServletContext().getRealPath(“/”)
使用
StreamSource的
InputStream
构造函数:

...new StreamSource(request.getServletContext().getResourceAsStream("lib/SorinCode.xsl"))
…如果web内容中有
lib/SorinCode.xsl
。如果它位于类路径中,请改用:

...new StreamSource(getClass().getResourceAsStream("lib/SorinCode.xsl"))

使用
StreamSource
InputStream
构造函数:

...new StreamSource(request.getServletContext().getResourceAsStream("lib/SorinCode.xsl"))
…如果web内容中有
lib/SorinCode.xsl
。如果它位于类路径中,请改用:

...new StreamSource(getClass().getResourceAsStream("lib/SorinCode.xsl"))

第一个是有效的,但是通过调用
getServletContext()
不是为
请求
对象,而是为
这个
对象。。。因此,解决方案应该是新的StreamSource(getServletContext().getResourceAsStream(“lib/SorinCode.xsl”)
第一个有效,但是通过调用
getServletContext()
不是为
请求
对象,而是为
。。。因此,解决方案应该是新的StreamSource(getServletContext().getResourceAsStream(“lib/SorinCode.xsl”)实际上我使用了
getRealPath()
来保存结果,所以感谢您的想法。。。因此,
StreamResult
newstreamresult(newfileoutputstream(getServletContext().getRealPath(“/”+“output.txt”))
。实际上我使用了
getRealPath()
来保存结果,所以感谢您的想法。。。因此,
StreamResult
newstreamresult(newfileoutputstream(getServletContext().getRealPath(“/”+“output.txt”))