java 读取web.xml,java - 从java类中的web.xml读取值 - SO中文参考 - www.soinside.com

8

投票

我找到了解决方案,实际上你必须在web.xml中声明一些env-entry标签,如下所示:

properties-file

java.lang.String

Property

在你的java类中,你必须导入Context和NamingException(这在我的情况下,我不确定这是否适用于其他人):

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

如果你想获得价值,你必须这样做:

Context ctx = new InitialContext();

Context env = (Context) ctx.lookup("java:comp/env");

final String fileName = (String) env.lookup("properties-file");

希望这也有助于其他人:-)

2

投票

在你的web.xml中添加一个init-param,如下所示 -

myParam

myParamValue

您可以使用以下代码访问代码:

getServletContext().getInitParameter("myParam")

1

投票

更简单:

veb.hml:

properties-file

java.lang.String

Property

java类:

InitialContext initialContext = new InitialContext();

String fileName = (String) initialContext.lookup("java:comp/env/properties-file");

0

投票

您可以使用Properties对象读取XML文件。

有关如何加载xml的示例代码如下所述:

File file = new File("test.xml");

FileInputStream fileInput = new FileInputStream(file);

Properties properties = new Properties();

properties.loadFromXML(fileInput);

fileInput.close();

加载文件后,现在可以使用properties.getProperty("keyname");获取属性

0

投票

我有类似的挑战;我不会详细说明,但足以说我试图绕过设置环境变量来设置logback日志记录路径。这不是我使用的解决方案,但它确实在web.xml中定位和读取并允许您获取变量。它使用JDOM2,因此您几乎可以阅读任何想要的内容。

private String getLogLocation() throws JDOMException, IOException {

SAXBuilder jdomBuilder = new SAXBuilder();

String logLocation = null;

Document jdomDocument = jdomBuilder.build(this.getFilePath());

Element webXMLRoot = jdomDocument.getRootElement();

List elements = webXMLRoot.getChildren();

for (Element e : elements) {

for (Element childElement : e.getChildren()) {

System.out.println(childElement.getQualifiedName());

if (childElement.getValue().equals("logLocation")) {

logLocation = e.getChild("env-entry-value",childElement.getNamespace()).getValue();

break;

}

}

}

return logLocation;

}

private String getFilePath() {

String classPath = LoggerStartupListener.class.getProtectionDomain().getCodeSource().getLocation().getPath();

classPath = classPath.replace("classes", "");

classPath = classPath + "web.xml";

return classPath;

}

-1

投票

你可以使用XPath找到你感兴趣的元素,例如

DocumentBuilder builder = DocumentBuilderFactory

.newInstance()

.newDocumentBuilder();

Document doc = builder.parse(file);

XPath xpath = XPathFactory

.newInstance()

.newXPath();

XPathExpression expr = xpath.compile("/web-app/servlet/servlet-name[text()='MyServlet']");

NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

编辑:根据您的评论,这里是一种获取web.xml文件句柄的方法。请注意,没有ServletContext,它取决于你从哪里调用它。所以这可能不适用于你的情况,但你可以调整它来获取文件。

File clsLocation = new File(getClass()

.getProtectionDomain()

.getCodeSource()

.getLocation()

.getFile());

String webXml = clsLocation

.getCanonicalPath()

.replaceAll("WEB-INF.*", "WEB-INF/web.xml");

File file = new File(webXml);

// use 'file' with the code above and change the XPath to the element you want to read

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值