aspose file tools*
The moose likes Servlets and the fly likes Init-Params and COntext-Params Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Init-Params and COntext-Params" Watch "Init-Params and COntext-Params" New topic
Author

Init-Params and COntext-Params

A Dev
Greenhorn

Joined: Dec 17, 2010
Posts: 5
Hey..

When do we use Init-params nad not context-params. please explain this with example.

I know the difference between them, but when there are Context-params, why do we need init-params??? using Context-params are always better than init-params as Entire Appln. can use it.
damodar kumar
Ranch Hand

Joined: May 19, 2008
Posts: 77

context param is for entire session of client so if you keep unnecessary things there it will become overhead for application
inti param is for that particular request or response

you need to differentiate where and when we can use those.


<a href="http://stackoverflow.com/users/668970/user668970" rel="nofollow">
<img src="http://stackoverflow.com/users/flair/668970.png" >
</a>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61240
    
  66

damodar kumar wrote:inti param is for that particular request or response

That is not correct. Init params apply to a particular servlet, not a single request.

Context params should be used when the data is independent of a particular servlet. Init params should be used when they are specific to a particular servlet.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
A Dev
Greenhorn

Joined: Dec 17, 2010
Posts: 5
damodar kumar wrote:context param is for entire session of client so if you keep unnecessary things there it will become overhead for application
inti param is for that particular request or response

you need to differentiate where and when we can use those.



Init-param is for specific servlet and context-param is for entire appln.... eve i too know this.

but, what i want to know is when there is context-param, why do we need init-param??? Context-param scores over init-param by being available entire appln. ....

init-param is used when it required for specific servlets.... why can't we use context-param here??? will it be overhead for Appln. if we use context-param??? if so, what sort of over-head???
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

It's a standard data-design procedure: you should give the data to the specific components which need it, instead of putting the data in a global data area which is accessible to all components, whether they need the data or not.

Let's suppose you have a parameter which a specific servlet needs, for example the address of a web service which it connects to. You seem to think it would be an advantage if all servlets had access to that information. I don't agree.

For one thing the other servlets don't have any use for it, so there's no advantage there.

For another thing the other servlets could possibly change or delete it, given bad enough programming, which is a disadvantage.

For yet another thing, if you had 67 servlets which connect to 67 different web services, then if those 67 names were in the global data area you would have to design carefully so that each servlet could tell which value to extract from the global data area. This is a disadvantage too.

So far I haven't seen anything for which putting all of the data in global storage is an advantage. Of course if you have data which applies to the entire application, then naturally that should be put into a place where the entire application can see it. But where's the advantages that you see to storing servlet-specific data at the application level?

(By the way waving your hands at unspecified "overhead" isn't much of an argument. You would have to describe what this "overhead" was, and why it mattered, and why there was significantly more of it when you stored servlet-specific data in the servlet as opposed to storing it in the application.)
A Dev
Greenhorn

Joined: Dec 17, 2010
Posts: 5
Thank you paul... your explanation is pretty clear. your answer echoed as best possible answer at this moment for my Question.

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

Another reason to add to Paul's list: you have a servlet specific setting; you make it available to all servlets as a context parameter; somebody else decides to use it in another servlet; you need to change the value...you've just broken the other servlet.
A Dev
Greenhorn

Joined: Dec 17, 2010
Posts: 5
Matthew Brown wrote:Another reason to add to Paul's list: you have a servlet specific setting; you make it available to all servlets as a context parameter; somebody else decides to use it in another servlet; you need to change the value...you've just broken the other servlet.


Hey Methew...

Your answer sounds like giving a string point...but, I didn't understand it clearly... Can you elaborate it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61240
    
  66

It's the same global variable problem... if you change it for one, you change it for all. Bad bad idea.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Init-Params and COntext-Params