Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java constants in JSP [duplicate]

Tags:

I have a class that defines the names of various constants, e.g.

class Constants {
    public static final String ATTR_CURRENT_USER = "current.user";
}

I would like to use these constants within a JSP without using Scriptlet code such as:

<%@ page import="com.example.Constants" %>
<%= Constants.ATTR_CURRENT_USER %>

There appears to be a tag in the Apache unstandard taglib that provides this functionality. However, I cannot find any way to download this taglib. I'm beginning to wonder if it's been deprecated and the functionality has been moved to another (Apache) tag library?

Does anyone know where I can get this library, or if it's not available, if there's some other way I can access constants in a JSP without using scriptlet code?

Cheers, Don

like image 825
Dónal Avatar asked Sep 24 '08 14:09

Dónal


1 Answers

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)

like image 111
ncgz Avatar answered Oct 20 '22 06:10

ncgz