This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Block in JSPs  RSS feed

 
Teresa Canales
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a couple of JSPs in the same directory that have the same static block. This static block get data from the database and stores the values in a static variable. I had to change the data so, I figured that I had the force the application server to reload all the JSPs. I only forced one of the JSPs and verified that the data was modified. I thought I would have to force the reload of the other JSPs but it turns out that they were reading the correct data. How are the other JSPs able to read the correct data when I didn't reload them? Is the static block memory shared accross all JSPs in the application? I thought that static block where only shared by instances of the class that contains the static block. Can someone clarify?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66143
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends what you mean by "static block".

That said, if you need to share some data across all resources (JSPs or servlets), a better place to store the data would be in the application context, rather then relying upon class-loader tricks like this.
 
Teresa Canales
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, using the application context would be better. I inherited these JSPs and they are in an application that needs to be restructured. That will be done in time. But I would still like to know the answer to my question. Are static variables or blocks in JSPs shared accross all the JSPs in the same directory. The static block pulls data from the database and stores the values in a static variable. I thought static variables are shared accross instances of the same class.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66143
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, depends what you mean. How about some code?
 
Teresa Canales
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code that we have on some of our JSPs. My question is basically when I force a reload on one of the that has this code in it, the other JSPs which are have not be reloaded are able to get the correct data. Why?

----CODE-----

private static Map stateProvMap = new HashMap();

static {
try {
StateProvinceAccessBean stateProvAB = new StateProvinceAccessBean();
Enumeration enum = stateProvAB.findByCountryNameAndLanguageId("United States", new Integer(-1));
while (enum.hasMoreElements()) {
stateProvAB = (StateProvinceAccessBean) enum.nextElement();
String state = stateProvAB.getName();
String abbr = stateProvAB.getStateAbbr();
stateProvMap.put(state, abbr);
}
stateProvMap = new TreeMap(stateProvMap);
} catch (Exception e) {
e.printStackTrace();
}
}

------------
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!