• 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

setting attribute for the entire application

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

I'm new to servlets. I know how to set an attribute to the session like:

But, how do i set it for the entire application. Do i have to use the init() method?..is there any other way of setting it without init()?. THanks for your replies.

Regards,
Mala
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Mala Sharma
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ben,

First of all, thanks alot for providing me that code. I just want to see if i understood it right. Lets say i make a class to which i pass all the attribute to be set for the entire application. Would it be something like this:


would that be Sufficient? THanks for clarifing this. I really appreciate it.

Mala
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mala Sharma:
Hello Ben,

First of all, thanks alot for providing me that code. I just want to see if i understood it right. Lets say i make a class to which i pass all the attribute to be set for the entire application. Would it be something like this:


would that be Sufficient? THanks for clarifing this. I really appreciate it.

Mala



You forgot the empty parenthesis after getServletContext.
It should have been getServletContext().setAttribute(....
Also, setAttribute doesn't trow any exceptions so the throws clause is unecessary unless you plan on adding code that does.

Non-servlet classes aren't going to have a getServletContext() method so you would need to pass the servlet context to that class somehow. As a rule I try to make all of my beans 'servlet unaware'. If you have a bean that requires a servletContext object in order to run, you won't be able to test it outside of the container.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you may want to do is make your 'AccessControl' object servlet-unaware and bind it (put it in) to the servletContext instead of trying to give it access to the servlet context.

Another alternative to using the servletContext is the 'singleton' design pattern. The advantage of this is that you'll never need to check for the existence of the object before creating it. In other words, you won't need to worry about two objects instanciating separate instances of the same object.
[ January 20, 2005: Message edited by: Ben Souther ]
 
Mala Sharma
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ben,
I really appreciate you helping me. I've got it working. I just called a servlet object from the main class that has all the servlet methods and then i just did this:

Thanks a lot for your help. You really helped me see the light, so to speak !L)
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you insuring that the called servlet has been loaded before making a call from another one? 'load-on-startup' in your servlet declaration?
 
Mala Sharma
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ben,

I took a different approach. I'm under the impression if i use the code mentioned above, that attribute will be there for the entire application after the FIRST request for that attribute?, that way, it will be loaded once after the first call, not before the call or when the application is first loaded. I'm doing this to load the drop down menu with some values from the database, so i wanted to query the database only once and then just use the attribute. We are using command servlet(we only have a class file not source code) and i can't write to it, thats why i took that approach. Is my thinking right?. I appreciate your comments. Thanks a lot.

Regards,
Mala
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not 100% sure what you meant.
If you want to fetch a list from the database when your app starts up and use that list for the life of the app without having to read it again, I would do it this way:
  • Create a context listener that reads from the database when the app is started.
  • Use the results from the database read to build an array (or whatever datastructure you like).
  • Load that list into a context scoped bean.
  • Use that bean to build your select box options when ever you need them.


  • [ January 20, 2005: Message edited by: Ben Souther ]
     
    reply
      Bookmark Topic Watch Topic
    • New Topic