当前位置: 首页 > 知识库问答 >
问题:

Tomcat web.xml配置变量

赵河
2023-03-14

我的Javaweb应用程序在war文件中包含了一个配置文件(例如myapp.xml),我在web.xml文件中提供了这个文件的路径,如下所示。

  <servlet>
    <servlet-name>wc</servlet-name>
    <servlet-class>com.myapp.servlets.WC</servlet-class>
    <init-param>
      <param-name>configfile</param-name>
      <param-value>${catalina.base}/webapps/myapp/WEB-INF/myapp.xml</param-value>
    </init-param>
  </servlet>

问题是,我现在想在同一台服务器上部署各种版本以进行测试和开发,每个版本在webapp下都有不同的路径。我想避免修改网页。xml文件,但我找不到任何方法来引用WEB-INF文件夹。

这是我想做的:

  <servlet>
    <servlet-name>wc</servlet-name>
    <servlet-class>com.myapp.servlets.WC</servlet-class>
    <init-param>
      <param-name>configfile</param-name>
      <param-value>${app.base}/myapp.xml</param-value>
    </init-param>
  </servlet>

其中 ${app.base} == ${catalina.base}/webapps/myapp/WEB-INF

是否有一些可用的变量可以为我提供应用程序目录的路径?

如果没有,你能想出其他方法来实现这一点吗?

共有2个答案

彭烨熠
2023-03-14

您可以使用

...
servletContext().getRealPath("");
...

获取应用程序的物理主目录。

张锐藻
2023-03-14

您可以将带有-Dapp.base=/foo/bar的变量传递到tomcat的JVM(取决于catalina.sh或 /conf文件中的版本),然后在WC servlet中使用System.get属性(…)读取它。

尽管建议将这些配置放在外部位置,这样您就可以在不接触已部署的应用程序的情况下更改它们,并在需要时启用未打包的WAR部署。

如果您想访问应用程序中的资源,那么您最好使用相对(而不是绝对)路径。

  • 如果资源在类路径上,请使用getClass().getResourceAsAstream(..)
  • 如果资源在类路径之外,请使用servletContext.getResourceAsStream(..)
 类似资料:
  • 如果不配置环境变量,每次登录 MySQL 服务器时就必须进入到 MySQL 的 bin 目录下,也就是输入“cd C:\Program Files\MySQL\MySQL Server 5.7\bin”命令后,才能使用 MySQL 等其它命令工具,这样比较麻烦。配置环境变量以后就可以在任意位置执行 MySQL 命令了,本节主要介绍怎样手动配置环境变量。 MySQL 如果不配置环境变量就只能在 my

  • These are used to override or specify the security settings when security is enabled. These are the possible settings: 用于当启动安全变量时覆盖或另行指定安全配置.有以下几种可能的配置: PHP_HANDLING - true/false. If set to true, the

  • If set to true, variables read in from config files will overwrite each other. Otherwise, the variables will be pushed onto an array. This is helpful if you want to store arrays of data in config file

  • This is a list of variables that are always implicitly assigned to the . This is handy for making global variables or server variables available to all templates without having to manually assign them

  • This is the directory used to store config files used in the templates. Default is "./configs", meaning that it will look for the configs directory in the same directory as the executing php script. 该

  • 类型: Object Nuxt.js 让你可以配置在客户端和服务端共享的环境变量。 例如 (nuxt.config.js): module.exports = { env: { baseUrl: process.env.BASE_URL || 'http://localhost:3000' } } 以上配置我们创建了一个 baseUrl 环境变量,如果应用设定了 BASE_URL