Is it possible to include a String from a Java file in a JSP? [duplicate]

On application startup, you can add the Constants class to the servletContext and then access it in any jsp page

servletContext.setAttribute("Constants", com.example.Constants);

and then access it in a jsp page

<c:out value="${Constants.ATTR_CURRENT_USER}"/>

(you might have to create getters for each constant)


Turns out there's another tag library that provides the same functionality. It also works for Enum constants.


Looks like a duplicate of accessing constants in JSP (without scriptlet)

My answer was:

Static properties aren't accessible in EL. The workaround I use is to create a non-static variable which assigns itself to the static value.

public final static String MANAGER_ROLE = 'manager';
public String manager_role = MANAGER_ROLE;

I use lombok to generate the getter and setter so that's pretty well it. Your EL looks like this:

${bean.manager_role}

Full code at https://rogerkeays.com/access-java-static-methods-and-constants-from-el