• Post Reply Bookmark Topic Watch Topic
  • New Topic

Application level global constants

 
Kinjal Sonpal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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 ]
 
M. Gagnon
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!