如何为Java Web应用程序设置参数 [英] How to set a parameter for a Java Web application

查看:190
本文介绍了如何为Java Web应用程序设置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java的Web应用程序,它使用一些外部程序(调用命令行工具)。

I have a web app in Java, which uses some external program (invokes a command line tool).

我想创建命令行程序的路径可配置,以便我可以在不重新构建我的应用程序的情况下进行更改。

I want to make the path of the command line program configurable, so that I can change it without re-building my application.

问题:

1)其中我应该使用的确切参数(在web.xml中可用的那个),如果它只设置一次(在部署时),之后永远不会改变?

1) Which exactly parameter should I use (out of those available in web.xml), if it is set only once (at deployment) and thereafter never changes?

2)怎么能我在Java代码中访问此参数?

2) How can I access this parameter inside my Java code?

提前致谢

Dmitri

推荐答案

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
  <context-param>
    <param-name>command</param-name>
    <param-value>SOME_COMMAND</param-value>
  </context-param>
.
.
.
.
</web-app>

Java代码

String commandToExecute =  getServletContext().getInitParameter("command");






另外

你也可以将这个东西放在类路径的property / xml文件中读取它,并在上下文初始化时将它放到servlet上下文中。

You can also put this thing in property/xml file in the classpath read it and put it to servlet context when context initializes.

这篇关于如何为Java Web应用程序设置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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