Class.getResourceAsStream的解释以及如何在Tomcat中为简单的Java Web应用程序设置它? [英] Explanation of Class.getResourceAsStream and how it is set in Tomcat for a simple Java web - app?

查看:210
本文介绍了Class.getResourceAsStream的解释以及如何在Tomcat中为简单的Java Web应用程序设置它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的java web应用程序,它被部署到Tomcat。

I have a very simple java web-application which is deployed to Tomcat.

在这个应用程序中,我有一些代码如下:

In this application I have some code which goes like this:

package com.mywebapp.hello;

import javax.servlet.http.*;
import java.io.*;

public class PdfTwoServlet extends HttpServlet {

    public void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {

        httpServletResponse.setContentType("application/pdf");
        InputStream is = PdfTwoServlet.class.getResourceAsStream("/two.pdf");

当我编译代码并将其部署到tomcat时,目录结构如下所示:

When I compile my code and deploy it to tomcat, the directory structure goes like this:

这就是说C:\ Tomcat \webapps \ myApplication:

This is under say C:\Tomcat\webapps\myApplication :

所以

PdfTwoServlet.class.getResourceAsStream("/two.pdf");

工作正常,找到类文件夹下的文件two.pdf,但我不知道如何这是有效的。

works fine and finds the file two.pdf which is under classes folder, but I have no idea how this works.

以编程方式访问JSF应用程序中的属性文件这里BalusC说:

Accessing properties file in a JSF application programmatically here BalusC says:


Class#getResourceAsStream ()可以采用一个相对于
的路径,你在那里使用的类的位置作为起始点。
如果使用/foo/filename.properties,它实际上会从类路径根加载
foo / filename.properties。

The Class#getResourceAsStream() can take a path which is relative to the location of the Class which you're using there as starting point. If you use /foo/filename.properties, then it will actually load foo/filename.properties from the classpath root.

我有两个问题:

1)为什么classpath根目录是WEB-INF\classes文件夹?它在哪里确定? (据我所知,应该是因为我说的代码工作正常。)

1) Why is the classpath root is WEB-INF\classes folder? Where is it determined? ( As far as I understand, it should be because code is working fine as I said. )

根据这个: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html ,I没有在我的本地计算机中设置类路径。所以也许当我启动tomcat时,它会设置类路径?但是部署了很少的Web应用程序,是否有很少的类路径?

According to this: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html , I do not have a classpath set in my local machine. So maybe when I start tomcat, it sets the classpath? But there are few web-apps deployed, are there few classpaths?

2)有没有更好的方法来完成这项工作而不是: PdfTwoServlet.class.getResourceAsStream ?像getClassPath()。getResourceAsStrem?

2) Is there a better way to make this work instead of: PdfTwoServlet.class.getResourceAsStream ? Something like getClassPath().getResourceAsStrem ?

编辑:也许有经验丰富且英语水平较高的人可以编辑此问题的标题。我不确定它是否足够好。

Maybe someone more experienced and with better English can edit the title of this question. I am not sure if it is good enough.

推荐答案

For 1)servlet应用程序中的类路径根目录是jar的WEB-INF\classes文件夹,加上那个WAR的WEB-INF / lib中所有jar的根。这些位置中的任何内容都将被视为类路径的根。

For 1) The classpath root in a servlet application is by specification the WEB-INF\classes folder of the jar, plus the root of all the jars in WEB-INF/lib of that WAR. Anything in those locations will be seen as the root of the classpath.

关于tomcat中的类路径如何工作的问题,当tomcat部署它时,按以下方式设置类路径:每个WAR对应一个可以访问WEB的单独的类加载器-INF / classes和WEB-INF / lib中的所有jar。

For the question on how classpaths in tomcat work, when tomcat deploys it set's the classpath in the following way: each WAR corresponds to a separate class loader which has access to WEB-INF/classes and all the jars in WEB-INF/lib.

默认情况下,如果在此处找不到搜索到的资源,将在tomcat / lib目录中搜索它。如果在那里找不到它,它将询问父类加载器,依此类推,可以找到解释这里

By default if the resource searched is not found here, it will be searched for in the tomcat/lib directory. If it's not found there, it will ask the parent class loader, and so on, the explanation can be found here

如果部署了多个Web应用程序,每个WAR都会拥有自己的类加载器,指向它自己的WEB-INF / classes和WEB-INF / lib jar。

If there are several web-apps deployed, each WAR will have it's own class loader pointing to it's own WEB-INF/classes and WEB-INF/lib jars.

对于2)没有类似getClasspath()的方法,ServletContext.getResourceAsStream()是建议的方式用于从WAR内部获取资源的servlet应用程序。 WAR可能会被压缩或爆炸,这对两者都有效,请参阅这个答案

For 2) there is not method like getClasspath(), ServletContext.getResourceAsStream() is the advised way in servlet applications for getting resources from inside the WAR. The WAR might be zipped or exploded, and this works for both, see this answer.

这篇关于Class.getResourceAsStream的解释以及如何在Tomcat中为简单的Java Web应用程序设置它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆