• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

JSP--Java class's static variable

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
I have strange problem with JSP.
I am using a static variable of a Java Class in JSP. If I change the variable value in Java class, compile it with out changing/re-compiling the JSP, that change is not getting effected. But when I re-compile JSP, the Java class's changes are getting affected.
Can anybody tell me what happens when we I build the project with no JSP change? (Is this going to take the Java Constants class's changes without re-compiling the JSP?)

Thanks in advance
 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Buddy,

First, welcome to the ranch! You need to change your name to comply with the JavaRanch Naming Policy.

Second, to answer your question... How are you accessing/using this static variable in your JSP page? Post some code and we can better help you.

Also, why are you using static variables? Do you have a variable that you want accessible by the entire web application? If so, we can probably point you to a better way...
 
Subha Murthy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,
Yes I am using the static variable throughout the application.
Here is my code:
jsp:
-------------------------------------------------------------
if((adminUserName.equals(Constants.XXX_ROLE_ADM_ID)) || (adminUserName.equals(Constants.XXX_ROLE_PMCRPT_ID))){%>
-------------------------------------------------------------

Constants.java
-----------------------------------------------------

Let me know if I am not clear still.

Thanks for your help
Subha
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, you are clear.

I believe (and other ranchers please correct me if I am wrong) that the reason the value does not change for you is because when the JSP is being compiled, it is simply inserting the actual value of this variable into the generated servlet code itself. As an example, if Constants.XXX_ROLE_ADM_ID is set to "Hello" in the Constants class, the generated java file would look like this:

This is assuming that the value is a public static final variable. Take a look at the generated java file and see what is says.

However, there is a better way to use an application wide variable, and it is checked with every request, so you won't run into this problem. You can set Context init parameters in your web.xml file that can be accessed by any jsp or servlet in your web application. Here is an example:


Now you can access it in your JSP through both scripting and EL (although I prefer EL):

Scripting:

EL (and JSTL):

Hope that helps!
 
reply
    Bookmark Topic Watch Topic
  • New Topic