我有一个带有web.xml文件的Web应用程序和一些我用Maven构建的java类。在某些时候,我想从java.xml类中获取web.xml文件中的参数。
如何读取普通java类中的值,而不是Servlet?
我找到了解决方案,实际上你必须在web.xml中声明一些env-entry标签,如下所示:
<env-entry>
<env-entry-name>properties-file</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>Property</env-entry-value>
</env-entry>
在你的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");
希望这也有助于其他人:-)
在你的web.xml中添加一个init-param,如下所示 -
<init-param>
<param-name>myParam</param-name>
<param-value>myParamValue</param-value>
</init-param>
您可以使用以下代码访问代码:
getServletContext().getInitParameter("myParam")
更简单:
veb.hml:
<env-entry>
<env-entry-name>properties-file</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>Property</env-entry-value>
</env-entry>
java类:
InitialContext initialContext = new InitialContext();
String fileName = (String) initialContext.lookup("java:comp/env/properties-file");
您可以使用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");
获取属性
我有类似的挑战;我不会详细说明,但足以说我试图绕过设置环境变量来设置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<Element> 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;
}
你可以使用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