如何从JSP页面访问环境变量 [英] How to access environment variables from JSP page
问题描述
如何从JSP页面访问环境变量?其中一个隐式对象是否允许访问它们?我找不到解决这个具体问题的例子。理想情况下,我正在寻找类似的东西:
< c:set var =where值= $ {myEnvironment.machineName} >
您可以在服务器启动时读取属性文件 - 使用
如果您正在寻找样本代码然后在这里找到它。请在下面的帖子中找到它。它可能对你有帮助。
-
< a href =http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm =nofollow noreferrer> JSP - 标准标签库(JSTL)教程
其他范围内的更多样本。
<% - 设置范围变量 - %>
< c:set var =paravalue =$ {41 + 1}scope =page/>
< c:set var =paravalue =$ {41 + 1}scope =request/>
< c:set var =paravalue =$ {41 + 1}scope =session/>
< c:set var =paravalue =$ {41 + 1}scope =application/>
<% - 打印值 - %>
< c:out value =$ {pageScope.para}/>
< c:out value =$ {requestScope.para}/>
< c:out value =$ {sessionScope.para}/>
< c:out value =$ {applicationScope.para}/>
在您的情况下,您已设置属性其中
默认页面
范围。
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:
<c:set var="where" value="${myEnvironment.machineName}">
You can read the properties file at the server start-up using ServletContextListener and store it as application scoped attribute to access it from anywhere in the application.
Steps to follow:
.properties:
machineName=xyz
web.xml:
<listener>
<listener-class>com.x.y.z.AppServletContextListener</listener-class>
</listener>
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:
Then you can just treat it as Map in EL.
${myEnvironment['machineName']}
or
${myEnvironment.machineName}
Read more about JSTL Core c:set
Tag
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.
The <c:set>
tag has following attributes:
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.
这篇关于如何从JSP页面访问环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!