Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

The Unstandard Tag Library: Using Constants Within JSPs

Dylan Barrett

Introduction

The Unstandard Tag Library is a JSP tag library that was developed as part of the Jakarta Project. Its purpose is to provide a collection of useful tags that people have been requesting for JSTL. The library serves as a place to keep all of these tags until they are officially added to the Standard Tag Library. This article focuses on the use of one of the tags contained within the library, the useConstants tag.

The Problem

While working on a project for a client, I realized that they were using a Java class with constants to define strings used within the application. The idea was to use the constants everywhere in the application where the strings were needed so they could easily be maintained in one location without having to search through the whole application when making a change. This makes sense, and is a common best practice. I was working on a JSP page that needed to use the constants from the class. It is now considered bad practice to use Java Scriptlets in your JSP page. It is much better to use the Expression Language (EL) in combination with JSTL and other custom tags. Trying to keep to this rule I quickly ran into a problem. The EL does not allow and easy way to access constants from a Java class. That is where the Unstandard Tag Library’s useConstants tag comes in handy.

How To Use It

Once you have added the library to your project it is very simple to use. Let’s assume we have a java class containing constants such as the one below:

package com.tallan.unstandard;  

public class MyConstants {    

  public static final String MY_CONSTANT_1 = 'Hello World';
  public static final String MY_CONSTANT_2 = 'Goodbye World';  

}

Let’s also assume that we added the tag library to our page as follows:

<%@ taglib uri="http://jakarta.apache.org/taglibs/unstandard-1.0"
           prefix="un"%>

We can then utilize the useConstants tag as follows:


After we run this line of code, an object called Constants will be available for use on the page. We can use it with the EL and access the constants as properties of the object. For example:


${Constants.MY_CONSTANT_1}

This solves our problem of using constants from a Java class within a JSP page without relying on scriptlets.

Where To Get It

You may be thinking “This is great, but where can I find this library?” Unfortunately the Jakarta project has stopped development of the library and no longer makes it obvious where you can get the code. If you look around on the Jakarta site you will find that they allow you to get the project out of source control and build it with Maven, but this is a little bit of a hassle. If you want an easier way to get the library just go here. This site contains old binaries of the library and they should work just fine.

Conclusion

The Unstandard Tag Library contains many useful tags that can help in the development of our JSP pages. While it is no longer being actively developed, it can still be a very useful tool. We covered how to access constants from a Java class using the library’s useConstants tag.

Want to Learn More?

If you want to learn more about the tags available in the Unstandard Tag Library and how to use them, then head over to the documentation page for the library located here.

1 Comment. Leave new

Thanks for the great post. Where in the JSP page would you put:

?

Thanks,
-s

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

\\\