I've gotten as far as this:
我已经达到了这个目的:
private Properties logoUrls = new Properties();
logoUrls.load(new FileInputStream("channelLogos.properties"));
where channelLogos.properties is in the same directory as my JSP. I get a FileNotFound exception. Where does my app actually think I mean by "channelLogos.properties", if not the same directory as the JSP? How can I determine the correct path to load the properties file?
其中channelLogos.properties与我的JSP位于同一目录中。我得到一个FileNotFound异常。我的应用程序实际上认为我的意思是“channelLogos.properties”,如果不是与JSP相同的目录?如何确定加载属性文件的正确路径?
5 个解决方案
#1
This will do the job:
这将完成工作:
<%@page import="java.io.InputStream" %>
<%@page import="java.util.Properties" %>
<%
InputStream stream = application.getResourceAsStream("/some.properties");
Properties props = new Properties();
props.load(stream);
%>
Anyway I really think you should have the properties file in the classpath and use a servlet
无论如何,我真的认为你应该在类路径中有属性文件并使用servlet
#2
I'd like to highly recommend reading about Model 2 Servlets. I recommend it to everyone who's still doing Model 1 Servlets, that is, doing "real work" in a JSP.
我强烈建议您阅读有关Model 2 Servlets的内容。我向所有仍在使用Model 1 Servlet的人推荐它,也就是说,在JSP中做“真正的工作”。
As to your question: First, throw the properties file in your classpath, then read the file using getResourceAsSttream:
关于你的问题:首先,在属性路径中抛出属性文件,然后使用getResourceAsSttream读取文件:
Thread.currentThread().getContextClassLoader().getResourceAsStream("channelLogos.properties");
There are many options, of course, and everyone will have their favorite.
当然,有很多选择,每个人都会有自己的最爱。
#3
When you say "same directory as JSP", what exactly do you mean by that? That your JSP sits somewhere in, say, /mywebapp/somefolder/my.jsp
with mywebapp
being your application root and your properties file is /mywebapp/somefolder/channelLogos.properties
?
当你说“与JSP相同的目录”时,你到底是什么意思?您的JSP位于某个位置,例如/mywebapp/somefolder/my.jsp,mywebapp是您的应用程序根目录,您的属性文件是/mywebapp/somefolder/channelLogos.properties?
If so, then most likely they're NOT in the same directory. JSP has been compiled and where it is actually located may vary depending on servlet container. Your best bet is to use ServletContext.getRealPath()
as suggested by pkaeding with properties file path relative to webapp context as an argument. Using the above example:
如果是这样,那么很可能他们不在同一个目录中。 JSP已经编译,它实际所在的位置可能因servlet容器而异。最好的办法是使用ServletContext.getRealPath(),如pkaeding所建议的,将相对于webapp上下文的属性文件路径作为参数。使用上面的例子:
private Properties logoUrls = new Properties();
logoUrls.load(new FileInputStream(servletContext.getRealPath("/somefolder/channelLogos.properties")));
That said, keep in mind that if you insist on putting your properties in the same folder as JSP you should take care to restrict it from being publicly accessible (unless that's the intention).
也就是说,请记住,如果您坚持将您的属性放在与JSP相同的文件夹中,您应该注意将其限制为可公开访问(除非这是意图)。
#4
Take a look at ServletContext.getRealPath(). That should give you the full path to the properties file.
看看ServletContext.getRealPath()。这应该为您提供属性文件的完整路径。
#5
JSP runs in servlet container, so its current working directory is defined by the container. Typically it is the directory where container is installed or its bin directory. In any case it is not the place where you want to store your custom properties file.
JSP在servlet容器中运行,因此其当前工作目录由容器定义。通常,它是安装容器的目录或其bin目录。在任何情况下,它不是您要存储自定义属性文件的位置。
There are 2 typical approaches to do what you need.
有两种典型的方法可以满足您的需求。
The first approach is good if your file is a part of your application and you never change it on deployment. In this case read it from resources:
如果您的文件是应用程序的一部分而您从未在部署时更改它,那么第一种方法很好。在这种情况下,从资源中读取它:
props.load(getClass().getResourceAsStream())
or even better
甚至更好
props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream())
*
The second approach is good if you want to change your properties file on deployment environment
如果要在部署环境中更改属性文件,则第二种方法很好
*. In this case put it somewhere in the file system outside your container. For example /opt/mycompany/conf/myproperties.properties on linux or at any other location you like. Now you should use absolute path when creating FileInputStream.
*。在这种情况下,将它放在容器外的文件系统中。例如linux上或您喜欢的任何其他位置的/opt/mycompany/conf/myproperties.properties。现在,您应该在创建FileInputStream时使用绝对路径。
To make system better configurable you should not write the path to configuration file inside the code. Better approach is to pass it to application using system properties, e.g. add parameter like -Dmycompany.conf=/opt/mycompany/myprops.properties when you are running your application server. When you want to read the file do the following:
为了使系统更易于配置,您不应该在代码中编写配置文件的路径。更好的方法是使用系统属性将其传递给应用程序,例如在运行应用程序服务器时添加类似-Dmycompany.conf = / opt / mycompany / myprops.properties的参数。如果要读取文件,请执行以下操作:
new FileInputStream(System.getProperties("mycompany.conf"))
Now configuration of your system can controlled independently by deployer.
现在,您的系统配置可以由部署者独立控制。
#1
This will do the job:
这将完成工作:
<%@page import="java.io.InputStream" %>
<%@page import="java.util.Properties" %>
<%
InputStream stream = application.getResourceAsStream("/some.properties");
Properties props = new Properties();
props.load(stream);
%>
Anyway I really think you should have the properties file in the classpath and use a servlet
无论如何,我真的认为你应该在类路径中有属性文件并使用servlet
#2
I'd like to highly recommend reading about Model 2 Servlets. I recommend it to everyone who's still doing Model 1 Servlets, that is, doing "real work" in a JSP.
我强烈建议您阅读有关Model 2 Servlets的内容。我向所有仍在使用Model 1 Servlet的人推荐它,也就是说,在JSP中做“真正的工作”。
As to your question: First, throw the properties file in your classpath, then read the file using getResourceAsSttream:
关于你的问题:首先,在属性路径中抛出属性文件,然后使用getResourceAsSttream读取文件:
Thread.currentThread().getContextClassLoader().getResourceAsStream("channelLogos.properties");
There are many options, of course, and everyone will have their favorite.
当然,有很多选择,每个人都会有自己的最爱。
#3
When you say "same directory as JSP", what exactly do you mean by that? That your JSP sits somewhere in, say, /mywebapp/somefolder/my.jsp
with mywebapp
being your application root and your properties file is /mywebapp/somefolder/channelLogos.properties
?
当你说“与JSP相同的目录”时,你到底是什么意思?您的JSP位于某个位置,例如/mywebapp/somefolder/my.jsp,mywebapp是您的应用程序根目录,您的属性文件是/mywebapp/somefolder/channelLogos.properties?
If so, then most likely they're NOT in the same directory. JSP has been compiled and where it is actually located may vary depending on servlet container. Your best bet is to use ServletContext.getRealPath()
as suggested by pkaeding with properties file path relative to webapp context as an argument. Using the above example:
如果是这样,那么很可能他们不在同一个目录中。 JSP已经编译,它实际所在的位置可能因servlet容器而异。最好的办法是使用ServletContext.getRealPath(),如pkaeding所建议的,将相对于webapp上下文的属性文件路径作为参数。使用上面的例子:
private Properties logoUrls = new Properties();
logoUrls.load(new FileInputStream(servletContext.getRealPath("/somefolder/channelLogos.properties")));
That said, keep in mind that if you insist on putting your properties in the same folder as JSP you should take care to restrict it from being publicly accessible (unless that's the intention).
也就是说,请记住,如果您坚持将您的属性放在与JSP相同的文件夹中,您应该注意将其限制为可公开访问(除非这是意图)。
#4
Take a look at ServletContext.getRealPath(). That should give you the full path to the properties file.
看看ServletContext.getRealPath()。这应该为您提供属性文件的完整路径。
#5
JSP runs in servlet container, so its current working directory is defined by the container. Typically it is the directory where container is installed or its bin directory. In any case it is not the place where you want to store your custom properties file.
JSP在servlet容器中运行,因此其当前工作目录由容器定义。通常,它是安装容器的目录或其bin目录。在任何情况下,它不是您要存储自定义属性文件的位置。
There are 2 typical approaches to do what you need.
有两种典型的方法可以满足您的需求。
The first approach is good if your file is a part of your application and you never change it on deployment. In this case read it from resources:
如果您的文件是应用程序的一部分而您从未在部署时更改它,那么第一种方法很好。在这种情况下,从资源中读取它:
props.load(getClass().getResourceAsStream())
or even better
甚至更好
props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream())
*
The second approach is good if you want to change your properties file on deployment environment
如果要在部署环境中更改属性文件,则第二种方法很好
*. In this case put it somewhere in the file system outside your container. For example /opt/mycompany/conf/myproperties.properties on linux or at any other location you like. Now you should use absolute path when creating FileInputStream.
*。在这种情况下,将它放在容器外的文件系统中。例如linux上或您喜欢的任何其他位置的/opt/mycompany/conf/myproperties.properties。现在,您应该在创建FileInputStream时使用绝对路径。
To make system better configurable you should not write the path to configuration file inside the code. Better approach is to pass it to application using system properties, e.g. add parameter like -Dmycompany.conf=/opt/mycompany/myprops.properties when you are running your application server. When you want to read the file do the following:
为了使系统更易于配置,您不应该在代码中编写配置文件的路径。更好的方法是使用系统属性将其传递给应用程序,例如在运行应用程序服务器时添加类似-Dmycompany.conf = / opt / mycompany / myprops.properties的参数。如果要读取文件,请执行以下操作:
new FileInputStream(System.getProperties("mycompany.conf"))
Now configuration of your system can controlled independently by deployer.
现在,您的系统配置可以由部署者独立控制。