在运行时修改Tomcat中的属性文件
我想在运行时修改 Portlet 的属性文件。该 portlet 部署在 Tomcat 7.0.23 中,属性文件位于“/WEB-INF/classes/content”中,我可以通过下面所示的代码访问它。事实上,代码执行时没有任何异常,但新添加的Property并没有保存到属性文件中。
String fileName = "MyProps.properties";
String relativePath = "/WEB-INF/classes/content/";
String fullPath = "c:/tomcat-7.0.23/webapps/my-portlet/WEB-INF/classes/content/";
try {
String path = relativePath + fileName;
InputStream in = getPortletContext().getResourceAsStream(path);
Properties props = new Properties();
props.load(in);
props.setProperty("test", "test");
File file = new File(fullPath + fileName));
FileOutputStream out = new FileOutputStream(file);
props.store(out, "");
} catch(Exception ex) { // error handling here}
添加新属性后,我可以验证
props.list(System.out);
它确实已添加。 context.xml 文件包含以下条目:
antiJARLocking="true"
antiResourceLocking="true"
这是在正在运行的 Tomcat 实例中添加/更改属性的正确方法还是我应该采取不同的方法?如果是后者,怎样才能最好地实现呢?
非常感谢您的回答!
I would like to modify a properties file of a Portlet during runtime. The portlet is deployed in a Tomcat 7.0.23 and the properties file sits in "/WEB-INF/classes/content" from where I can access it via the code shown below. In fact, the code is executed without any exceptions but the newly added Property is not saved to the properties file.
String fileName = "MyProps.properties";
String relativePath = "/WEB-INF/classes/content/";
String fullPath = "c:/tomcat-7.0.23/webapps/my-portlet/WEB-INF/classes/content/";
try {
String path = relativePath + fileName;
InputStream in = getPortletContext().getResourceAsStream(path);
Properties props = new Properties();
props.load(in);
props.setProperty("test", "test");
File file = new File(fullPath + fileName));
FileOutputStream out = new FileOutputStream(file);
props.store(out, "");
} catch(Exception ex) { // error handling here}
After adding the new Property, I could verify with
props.list(System.out);
that it was added indeed. The context.xml file contains the following entries:
antiJARLocking="true"
antiResourceLocking="true"
Is this the right way to add/change Properties in a running Tomcat instance or should I take a different approach? If the latter, how could it be achieved best?
Many thanks for your answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您绝对不应该依赖于能够更改已部署的 Web 应用程序中包含的文件。此时控制权已移交给容器,文件可能会被覆盖,或者甚至可能不可写。它还给应用程序部署人员带来了负担,因为现在他们不能简单地清除分解的 WAR 文件夹(如果存在)并重新部署存档。
作为您的方法的替代方法,请考虑将属性文件放置在 Web 应用程序外部的位置。我见过成功使用的一种方法是:
被托管,或允许部署者通过井指定此位置
已知财产。
不存在,从存储在其中的模板创建并初始化它
您的申请。
外部属性文件。
通过这种设置,您永远不必担心无法写入文件,或者它被容器覆盖。
You should definitely not rely on ever being able to change a file contained in a deployed web app. Control has been handed over to the container at that point and the file may be overwritten, or it may not even be writable. It also puts a burden on the application deployer, because now they cannot simply blow away the exploded WAR folder (if one exists) and redeploy the archive.
As an alternative to your approach, consider placing the properties file in a location external to the web app. One approach I've seen used successfully is this:
be hosted, or allow the deployer to specify this location via a well
known property.
not exist, create and initialize it from a template stored within
your application.
external property file.
With this setup you never have to worry about not being able to write the file, or of it getting overwritten by the container.