5

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}">
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user3120173
  • 1,758
  • 7
  • 25
  • 39
  • 1
    How do you define these *environment variables*? – Luiggi Mendoza May 16 '14 at 21:29
  • when you say environment. Do you mean the OS or the JVM ? – Brandt Solovij May 16 '14 at 21:29
  • Possible duplicate of [this](http://stackoverflow.com/questions/10463323/getting-environment-variables-value-in-java) – Wundwin Born May 16 '14 at 21:35
  • Either an **environment variable** http://en.wikipedia.org/wiki/Environment_variable or a **system property** http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html Does that help? And yes I know that **environment variables** and **system properties** are different, I'm just looking for some way to reach outside the JSP page and look at them. – user3120173 May 16 '14 at 22:06
  • @suninsky - no this is **not** a duplicate. – user3120173 May 16 '14 at 22:08
  • @user3120173 What are you looking for? What do you mean by `machineName` here? Are you talking about client machine name or server machine name? – Braj May 16 '14 at 22:31
  • My initial post was designed to be as brief as possible, however I've now concluded that was a terrible mistake. I was not looking for information on Core JSTL, the tag, or whether I could put something in the value="whatever" field. I simply wanted to know if I could access environment variables from any of the implicit JSP/JSTL objects (pageContext, application, etc.) Apparently my question was so short and poorly worded that it mystified everyone for which I apologize to you personally and everyone in general. – user3120173 May 16 '14 at 22:49

1 Answers1

6

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:

enter image description here

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.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • I don't think this answers the question, which seems to be can you put an environment variable in the value="" clause? I get the impression from the above that the answer is no, but it never answers the question directly. – developerwjk May 16 '14 at 22:28
  • @developerwjk Yes I am just updating my post. – Braj May 16 '14 at 22:29