File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes setting attribute for the entire application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "setting attribute for the entire application" Watch "setting attribute for the entire application" New topic
Author

setting attribute for the entire application

Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
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
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410



Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Oct 29, 2004
Posts: 76
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Oct 29, 2004
Posts: 76
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

Joined: Dec 11, 2004
Posts: 13410

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 ]
     
    wood burning stoves
     
    subject: setting attribute for the entire application
     
    Similar Threads
    Session timeout - How to set for an application?
    Session attr. thread safety
    Session Variable
    JVM memeory allocation
    Oracle Portlet Session