wood burning stoves 2.0*
The moose likes Servlets and the fly likes ServletContext and properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContext and properties file" Watch "ServletContext and properties file" New topic
Author

ServletContext and properties file

Marc van Dorenmalen
Greenhorn

Joined: Jul 13, 2011
Posts: 1

Hello everyone,

I would like to show you how my colleague and I had a little question. The question was, how to set the properties within a .properties file made available in the init for the entire web application. The benefits of this are:
-One time loading the parameters for the web application
-The parameters are loaded within the init phase
-It is user friendly to edit the parameters, I don't want to make use of the <init-params> tag.

So, what we want is to load a properties file in the init method. We have found the following answer:

1- Read properties file via the ServletContext
2- Set these properties as attribute
3- Get the attributes within a servlet

Because at this time we had one servlet, that is it. If we have more servlets, then we have to make sure this servlet is the first one to load on startup. Therefore we can set the <load-on-startup> tag in the DD file.

A part of the code is like this:

public void init(ServletConfig conf) throws ServletException {
InputStream is = conf.getServletContext().getResourceAsStream("/WEB-INF/test.properties");
Properties props = new Properties();
props.load(is);
conf.getServletContext().setAttribute("test.properties",props);
}
catch(IOException ioe) {
e.printStackTrace();
}
super.init(conf);
}


Then within the doGet method:

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
Properties props = (Properties) getServletContext().getAttribute("test.properties");
}


My question to you:
What do you think of it?
Are there alternatives with the described benefits?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

That would have been fine 10 years ago. In a modern web app a context listener should be used instead of a servlet to load configuration data when the web application starts.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kunalkumar somani
Greenhorn

Joined: Jul 13, 2011
Posts: 7

Hi Marc van,

Bear Bibeault is correct this one is very old. And one more thing why you want to store in ServletContext, you are increasing the size of the object and ServletContext object is available in the whole application.

And still you want to load one time only and want to use in the whole application thn Create singleton object and load the property file in consturctor.



Hope works well....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

Singletons should be avoided. The proper place to store information that is to be shared amongst all resources of a web application is the application scope (ServletContext). The argument that this "takes up space" is specious -- whether the data is stored in application scope or by a singleton is moot -- it will take up the same amount of storage.
kunalkumar somani
Greenhorn

Joined: Jul 13, 2011
Posts: 7

Dear it takes same storage even i would say less storage but do you really think we need to store in application context? no and never please try to understand application object is available in the whole application, even if you dont required object you are storing in application context and tht way you are increasing the size of application context object and it is very costly.

Think on size like this way as well, first you are creating one object, this object also taking memory than you are storing in application context as well again, you are increasing the application context object size.

And singleton is not a bad idea. if you want to become a good programer and improve your application performance, you should use maximum java design pattern.

Singleton is one of the java design pattern.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

kunalkumar somani wrote:Dear it takes same storage even i would say less storage but do you really think we need to store in application context? no and never please try to understand application object is available in the whole application, even if you dont required object you are storing in application context and tht way you are increasing the size of application context object and it is very costly.

No matter where the data is stored, it will use memory. Whether a data structure is stored in a reference from a singleton, or within the application context is completely irrelevant The data will still take up the same amount of memory.

Are you seriously saying that storing the data in a singleton reference will take less memory than storing it elsewhere?

And singleton is not a bad idea. if you want to become a good programer and improve your application performance, you should use maximum java design pattern.

This is a common naive misconeption. Using a pattern just to use the pattern is bad idea. There is no need to use a singleton in this instance. Storing application-wide data is exactly what the application context is intended for and should be used as such.

Singleton is one of the java design pattern.

So? So is the Decorator pattern. But all patterns should be used appropriately and not just so you can say that you used a pattern. Singletons, in fact, are one of the most abused of the patterns.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

excellent explanation bear
kunalkumar somani
Greenhorn

Joined: Jul 13, 2011
Posts: 7

I'm not telling you that "storing the data in a singleton reference will take less memory than storing it elsewhere". I mean to say first you are creating one object it takes memory than the same object you are storing in another object tht increase the size of that object(where you stored) as well.

And please don't tell singleton is one of the most abused of the patterns.

Think about the perfomance please. If you are storing the object in context and whenever you want to read the property, first you have to get the object from the context, means you have to write getAttribute method and convert that object into Properties object and thn get the property.

You are increasing the line of code as well and compiler has to do more operation. In real time application client can not afford performance issue. And still you beleive that storing in context object is good idea please go ahead.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

kunalkumar somani wrote:I'm not telling you that "storing the data in a singleton reference will take less memory than storing it elsewhere". I mean to say first you are creating one object it takes memory than the same object you are storing in another object tht increase the size of that object as well.

I don't really know what you're saying here, but once again, it makes no difference where the object is stored, it will use up the same amount of memory. Which object the data is referenced by makes no difference in the total memory usage.

And please don't tell singleton is one of the most abused of the patterns.

I most certainly will. Just like I will continue to harp on other bad practices such as using scriptlets in a JSP, writing HTML in a servlet, and misusing any other Java element.

Singletons have their place, but they are overused, and the particular issue discussed in this topic is certainly not one that a Singleton should be used for. The entire concept of the application context was created just for this purpose and is the proper tool to use in this case.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: ServletContext and properties file
 
Similar Threads
Regarding Service, SevletConfig and ServletContext
deploy webservice which access application level object
log4j...
Doubt in JSP Custom tags (Simple Tag) in Dynamic Drop Down
Getting HttpSession from the init method