Java 如何从 JSP 页面访问环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23704812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access environment variables from JSP page
提问by user3120173
How can I access environment variables from a JSP page? Does one of the implicit objects give access to them? I couldn't find an example addressing this specific issue. Ideally I'm looking for something like:
如何从 JSP 页面访问环境变量?隐式对象之一是否可以访问它们?我找不到解决此特定问题的示例。理想情况下,我正在寻找类似的东西:
<c:set var="where" value="${myEnvironment.machineName}">
采纳答案by Braj
You can read the properties file at the server start-up using ServletContextListenerand store it as applicationscoped attribute to access it from anywhere in the application.
您可以在服务器启动时使用ServletContextListener读取属性文件,并将其存储为应用程序范围的属性,以便从应用程序的任何位置访问它。
Steps to follow:
要遵循的步骤:
.properties:
。特性:
machineName=xyz
web.xml:
网页.xml:
<listener>
<listener-class>com.x.y.z.AppServletContextListener</listener-class>
</listener>
AppServletContextListener.java:
AppServletContextListener.java:
public class AppServletContextListener implements ServletContextListener {
private static Properties properties = new Properties();
static {
// load properties file
try {
// absolute path on server outside the war
// where properties files are stored
String absolutePath = ..;
File file = new File(absolutePath);
properties.load(new FileInputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
servletContextEvent.getServletContext().
setAttribute("myEnvironment", properties);
}
}
JSP:
JSP:
Then you can just treat it as Map in EL.
然后你可以把它当作 EL 中的 Map 。
${myEnvironment['machineName']}
or
或者
${myEnvironment.machineName}
Read more about JSTL Core c:set
Tag
阅读有关JSTL 核心标签的更多信息c:set
The <c:set>
tag is JSTL-friendly version of the setProperty
action. The tag is helpful because it evaluates an expression and uses the results to set a value of a JavaBean
or a java.util.Map
object.
该<c:set>
标记是该setProperty
操作的JSTL 友好版本。该标记很有用,因为它计算表达式并使用结果设置 aJavaBean
或java.util.Map
对象的值。
The <c:set>
tag has following attributes:
该<c:set>
标签具有以下属性:
If target is specified, property must also be specified.
如果指定了目标,则还必须指定属性。
Read more about it HERE
在此处阅读更多相关信息
If you are looking for sample code then find it here. Please find it at below posts. It might help you.
如果您正在寻找示例代码,请在此处找到它。请在下面的帖子中找到它。它可能会帮助你。
More samples on other scopes.
有关其他范围的更多示例。
<%-- Set scoped variables --%>
<c:set var="para" value="${41+1}" scope="page" />
<c:set var="para" value="${41+1}" scope="request" />
<c:set var="para" value="${41+1}" scope="session" />
<c:set var="para" value="${41+1}" scope="application" />
<%-- Print the values --%>
<c:out value="${pageScope.para}" />
<c:out value="${requestScope.para}" />
<c:out value="${sessionScope.para}" />
<c:out value="${applicationScope.para}" />
In your case you have set an attribute where
in default page
scope.
在您的情况下,您已where
在默认page
范围内设置了一个属性。