• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Application level global constants

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I hope this question belongs to current forum.

I need to access certain constants (those typical public static final ones), which are global across the application, including other layers. How do I use these in jsp pages and where do I declare them?

I have thought of putting the Interface (having constants) in application scope, by a servlet. But I'm not sure whether this is the correct approach, or there are better/smoother approaches. Is it possible to configure this in web.xml?

JFYI, I'm using struts framework and using that framework, it may allow me to do it, but I'd like to avoid any framework specfic implementation.

Please share your thoughts on this.

Thanks and regards,
Kinjal Sonpal
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your value is declared "public static final" you don't have to put a reference to the class (or interface) *anywhere*. Since they are declared as static, you can access them directly through the class.

(Later...)
Actually, I think I misunderstood your question...

You want something that behaves *like* public static final references... you might want to look into using a ServletContextListener. ( Here is a short tutorial on how to use the various listeners and declare them in the deployment descriptor. )

These aren't exactly like public static *final* values, because they can be changed... they're more like public static only.
[ July 07, 2004: Message edited by: Nathan Pruett ]
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kinjal Sonpal:

I need to access certain constants (those typical public static final ones), which are global across the application, including other layers. How do I use these in jsp pages and where do I declare them?

I have thought of putting the Interface (having constants) in application scope, by a servlet. But I'm not sure whether this is the correct approach, or there are better/smoother approaches. Is it possible to configure this in web.xml?



There are probably several ways to do this, but one solution is to define a class (not an interface) with all your static finals that you will use as a bean with application scope. You can instantiate it in the servlet and add it to the servlet context with

getServletContext().setAttribute("objName", obj)

or you can put a useBean tag in the JSP with application scope:

<jsp:useBean id="beanName" class="MyPackage.MyConstantsClass" scope="application" />

Then you can refer to the constants in your JSP code as an expression

<%= constantsBean.MY_CONSTANT_1 %>

or with the expression language of JSP 2.0

${constantsBean.MY_CONSTANT_1}

Sorry about the empty post above.
 
Kinjal Sonpal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nathan Pruett:
If your value is declared "public static final" you don't have to put a reference to the class (or interface) *anywhere*. Since they are declared as static, you can access them directly through the class.

Nathan, I'm actually looking for what you understood initially. The above approach is good, but I think I'll have to make explicit imports in all jsp pages to use the constants class. I'm looking for a more maintainance-free solution, such that individual pages need not worry about importing or including them.

Actually, the reason behind this is I have a good number of string constants in use. Currently the application is not more than 40 odd pages, but very soon we are going to expand.

I'll try the solution given in the next post. I think it should work.

Btw, the other part of your post is also very interesting. May be I'll try to incorporate both for my requirement.

Thanks and regards,
Kinjal Sonpal
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic