What is the best way to provide a server-side property to a JSP?

230 Views Asked by At

I have a system.properties file with things like system version and build type that I'd like to show on my index.jsp pages (I have several in a suite of dashboards). What is the best way to provide those properties to my JSPs?

I'm currently reading the properties file directly from the JSPs, but that's cumbersome because it's several lines of code and it has to be duplicated across all of my JSPs. I did abstract that code out into its own JSP that I then include on my other JSPs, but that still feels cumbersome.

Ideally, I'd like to do the following from any page:

<body data-build-type='${buildType}' data-system-version='${systemVersion}'>

That could mean that I use a servlet or a servlet-filter, but I'm not sure.

Thanks!

2

There are 2 best solutions below

3
On BEST ANSWER

You could use a WebListener that implements ServletContextListener.
So on deployment you could read the system properties and set them as attributes.
Either separately or in a map.

system.properties:

Lets say you have a file system.properties with content:

buildType=myType
systemVersion=v55

WebListener:

The WebListener could be something like:

package testingThings.properties;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;  
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class ContextListener implements ServletContextListener {
    public ContextListener() {}
    public void contextDestroyed(ServletContextEvent sce) {}

    public void contextInitialized(ServletContextEvent sce) {
        InputStream stream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("testingThings/properties/system.properties");
        Properties props = new Properties();

        try {
            props.load(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        HashMap<String, String> map = new HashMap<String, String>();

        for (final String name : props.stringPropertyNames()) {
            map.put(name, props.getProperty(name));
        }

        sce.getServletContext().setAttribute("system", map);
    }
}

JSP:

In a JSP you can iterate through the system attribute like this:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head><title>access map of system properties</title></head>
<body>
    <h3>access map of system properties</h3>
    <table>
        <c:forEach items="${system}" var="property">
            <tr>
                <td>${property.key}:</td>
                <td>${property.value}</td>
            </tr>
        </c:forEach>
        <tr>
    </table>

    <h3>directly access of map properties</h3>
    <table>
            <tr>
                <td>buildType:</td>
                <td>${system.buildType}</td>
            </tr>
            <tr>
                <td>systemVersion:</td>
                <td>${system.systemVersion}</td>
            </tr>
        <tr>
    </table>
</body>

If the system properties are changing dynamically,
you can update them, directly in the context attribute (parallel to the system.properties file)

0
On

Create a JSP custom tag. For example:

public class HelloWorld extends TagSupport
{
    public int doStartTag() throws JspException
    {
        try
        {
            JspWriter out = pageContext.getOut();
            HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
            out.write("Hello world!"); <!-- your property 
        }
        catch(Exception e)
        {   
            throw new JspException(e.getMessage());
        }
        return EVAL_PAGE;
    }
}
        <!-- a tab library descriptor -->
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name>Simple Tags</short-name>

  <tag>
    <name>HelloWorld</name>
    <tag-class>HelloWorld</tag-class>
    <body-content>empty</body-content>
  </tag>
</taglib>

Using it in your JSP:

<html>
  <body>
    <hw:HelloWorld />
  </body>
</html>

Hello World is just an example - you can have all of your attributes (system properties) defined in your tag and get those in the JSP this way. See: http://docs.oracle.com/javaee/5/tutorial/doc/bnalj.html