Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Where to put global objects in Java web app  RSS feed

 
Ranch Hand
Posts: 1930
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was not sure what to put in subject line and not sure this is the correct forum.

In my web app I have a function that runs when first loading where I create several(50+) class objects that hold various lists that will be used in the application. I assign this function output to a session attribute so the app can access it.

For example a list of supplier names and data. This list is populated from a remote data result.

Each time a user accesses the application(separate session of the application) this list would be created for that session. Or I assume this is what is happening.

Here is my application design question. Is there a way that these lists could be at some global level in the application so each session would not have to construct its own class object of the list?

Is this sort of thing a candidate for a Web Service? Would this be more efficient? Or is the way I have it designed the correct way?

I an trying to reduce the heap size of the application.  
 
Marshal
Posts: 24184
54
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me that if those lists belong to the application, and aren't specific to any particular user, then you should be putting them in application scope rather than in session scope. That way you'd only have to create them once, when the application starts, instead of having to create them every time a new session is created. Likewise there'd be only one copy of the lists instead of having a copy for each active session.

Note: this assumes that the contents of the lists don't ever change. If they do, then you need to have a way for the application to reload the lists when such a change happens. This can be done, of course, but it's something that needs to be included in your application's design.
 
Steve Dyke
Ranch Hand
Posts: 1930
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote: ...then you should be putting them in application scope rather than in session scope...



First thanks for your help in this matter.

So would the lists get loaded when I started the application server? If changes were made I could just restart the server.

Is there a certain way or place I put the list so they belong to the application? I am using WebSphere and eclipse. Or do I have to do this on the application server?
 
Paul Clapham
Marshal
Posts: 24184
54
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Execute code on webapp startup and shutdown using ServletContextListener

To that I would add: from the ServletContextEvent object passed to the contextInitialized method you can get a ServletContext; to add an object to application scope you can call its setAttribute(name, object) method. Elsewhere in your app you can easily get a ServletContext and call getAttribute(name) to retrieve that object, or in JSTL it's very easy to access objects in application scope.
 
Steve Dyke
Ranch Hand
Posts: 1930
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Execute code on webapp startup and shutdown using ServletContextListener

To that I would add: from the ServletContextEvent object passed to the contextInitialized method you can get a ServletContext; to add an object to application scope you can call its setAttribute(name, object) method. Elsewhere in your app you can easily get a ServletContext and call getAttribute(name) to retrieve that object, or in JSTL it's very easy to access objects in application scope.



Still not sure I am doing this correctly:

I have my listener created and a list added to it. How do I access the list in my app?

 
Marshal
Posts: 67239
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ServletContext.getAttribute()

Of course, you've got to put it there first.

Think about it, of what use is sticking it into a member variable of the listener? You want to put all your "global" data into the application context (aka ServletContext).
 
Paul Clapham
Marshal
Posts: 24184
54
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:... from the ServletContextEvent object passed to the contextInitialized method you can get a ServletContext; to add an object to application scope you can call its setAttribute(name, object) method.

 
Steve Dyke
Ranch Hand
Posts: 1930
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:ServletContext.getAttribute()

Of course, you've got to put it there first.

Think about it, of what use is sticking it into a member variable of the listener? You want to put all your "global" data into the application context (aka ServletContext).



This is what I have now and it seems to work when I use it in my JSTL code.

 
Saloon Keeper
Posts: 20498
115
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Long before there were webapps, or for that matter, even the Internet, I learned the hard way that global variables can be a real nightmare. So keep them to a minimum.

It sounds like you're looking to keep static lists that might be used as HTML SELECT option lists or their equivalent, and yes, that's something that's reasonable to put in an easily-located shared scope, especially since they're normally not going to be updated, only read.

For that sort of thing, the Application Scope is preferable.

You can put them in other places - for example as static class members - but I don't recommend it. Application Scope is the first place I'd want to look for them, and you get a bonus in that you can locate them via a logical bean name instead of having to hard-code a path. And the JEE API's make it easier to work with them in stuff like EL expressions on your View Template of choice, be it JSP, a JSF xhtml template or whatever.
 
Steve Dyke
Ranch Hand
Posts: 1930
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Long before there were webapps, or for that matter, even the Internet, I learned the hard way that global variables can be a real nightmare. So keep them to a minimum.



Thanks for the insight. My quest is to reduce the number of objects having to be created on the heap. I have "Out of Memory" issues from time to time. I thought that minimizing the number of times a class is constructed might help.

My JVM is set to 3G already. My compiled app(deployed) size is almost 1G. The app will run for several days even a week or more then the heap size grows beyond the max.
 
Steve Dyke
Ranch Hand
Posts: 1930
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steve Dyke wrote:

Tim Holloway wrote:Long before there were webapps, or for that matter, even the Internet, I learned the hard way that global variables can be a real nightmare. So keep them to a minimum.



Thanks for the insight. My quest is to reduce the number of objects having to be created on the heap. I have "Out of Memory" issues from time to time. I thought that minimizing the number of times a class is constructed might help.

My JVM is set to 3G already. My compiled app(deployed) size is almost 1G. The app will run for several days even a week or more then the heap size grows beyond the max.



I think I may have a different approach to the more dynamic lists. The app contains what I call modules, set up as a navigation menu. Not every user needs access or has access to every module.
They can pick and choose which ones they want in their menu to a certain extent.
In the log on servlet I could analyze the module criteria and determine which lists it would need and load these into session attributes.

As I have stated before, currently every list loads into a default class set to a session attribute when the first servlet in app is called even if it is never accessed.
 
Tim Holloway
Saloon Keeper
Posts: 20498
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steve Dyke wrote:In the log on servlet



 
Steve Dyke
Ranch Hand
Posts: 1930
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Steve Dyke wrote:In the log on servlet





Can you elaborate please?
 
Tim Holloway
Saloon Keeper
Posts: 20498
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a macro key for this, I type it so often.

J2EE/JEE define a standard container-managed security system. It's designed by security professionals, well-documented, thoroughly debugged, and - most importantly - to the best of my knowledge, it has never been cracked. It has the additional virtue that it blocks a lot of attacks before they can even reach application code, so they cannot exploit possible weaknesses in the application code. And, of course, it's built into the JEE API.

Do-it-yourself security systems - where you design and implement  your own authentication (login) and authorization functions have an absolutely atrocious history for being crackable. Many, in fact, can be bypassed in under 15 minutes by unskilled persons.

Unless you are professionally-trained, full-time security, you really shouldn't even think about designing your own security system. Security systems are as strong as their weakest link and unless you know what you're doing, there's a virtual certainty that you'll have at least one. Even the pros get caught occasionally. It's definitely not something you should be doing "in addition to the important stuff". It's not even something you should expect the local "genius" to be able to do properly.
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!