• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using constants  RSS feed

 
Trader Joey
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have declared an interface that defines a set of constants. ( public final .... ). I wish to use these constants on a JSP page. Normally I would implement the interface in the class I wish the use the constants in. But how is this going to work for JSPs ?
Thanks
Nikhil.
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do it this way:
- create a class that encapsulates your constants instead of an interface, there is an argument against using interfaces purely as a mechanism for providing constants (but can't remeber it off the top of my head, something like it breaks some OOD design rule)

Then just import it into your jsp and make tests such as:
if (myVar1 == MyConstants.MY_CONSTANT1) {
...
}
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66152
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using constants in this manner, especially for string contants like request parameters which must match across classes, and that will cause difficult-to-debug problems if transposed incorrectly is a very good practice that I don't see a whole lot of.
Good deal!
bear
 
Ruilin Yang
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not put these constants in a java bean ? Then you can call it any time / any page.
 
intenter
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I have same problem. I need to refer static const value in JSP.
But there is one condition: not using scriptlets.
Whow to refer constant using JSTL or EL?
Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!