• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why config is read only???

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was arranging scope objects into assending order like this:

1] page
2] request
3] session
4] config
5] context

But one of my friend told me that config is not scope object because it is read only and you can't write anything into config at run time. If this is right then why it is so? We have access of config in service() method (I think) so why not we are allowed to write something in config and that data will be for that servlet. All pages, all requests and all sessions (all users) will have the same config data.

I hope it is logical and making sense.
Please comments.

Thanks.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are confusing the config object with the context object. Config is used to access initialization parameters (configured in the web.xml file). You can't write to it because that would mean modifying the web.xml... There is one ServletConfig object per servlet.

Context is shared throughout the web application, and is accessible by every servlet in the web app. This is where you would write the data to be shared.

Check out the ServletConfig and ServletContextAPIs.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But one of my friend told me that config is not scope object


He is correct.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Bourdeaux:
You are confusing the config object with the context object. Config is used to access initialization parameters (configured in the web.xml file). You can't write to it because that would mean modifying the web.xml... There is one ServletConfig object per servlet.

Context is shared throughout the web application, and is accessible by every servlet in the web app. This is where you would write the data to be shared.

Check out the ServletConfig and ServletContextAPIs.


Thanks Paul,
I know that writing in config means modifying web.xml. But why we are not allowed to do that like any other scope object?

Please comments.
Thanks.

 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because config is not a scope object

1. request
2. session
3. application(context)
4. page

are the only available four scopes, with which you can get and set attributes.
 
subho saha
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the basic way to answer this topic is to point out the difference between Attribute and Parameter.You can set attributes in scope.I.e is you can read and write attributes in context,session ,request scope.But for parameter you can only read them from web.xml bcoz they are fixed except other than request parameter's.
[ October 24, 2005: Message edited by: subho saha ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishnu Prakash:
Because config is not a scope object

1. request
2. session
3. application(context)
4. page

are the only available four scopes, with which you can get and set attributes.


Yes but why?
Thanks.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by subho saha:
I think the basic way to answer this topic is to point out the difference between Attribute and Parameter.You can set attributes in scope.I.e is you can read and write attributes in context,session ,request scope.But for parameter you can only read them from web.xml bcoz they are fixed except other than request parameter's.

[ October 24, 2005: Message edited by: subho saha ]


Sorry, I am not able to relate this two. Could you please go in bit depth.
Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes but why?


That's like asking why a banana isn't a make of automobile. The servlet config is not a scope object because it wasn't defined and designed as so.

Sorry, I am not able to relate this two.


That's because there really is no relation. Context and init parameters (those defined within the web.xml file) have nothing at all in common with request parameters except the term "parameter".
[ October 24, 2005: Message edited by: Bear Bibeault ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:

Sorry, I am not able to relate this two. Could you please go in bit depth.
Thanks.


"Config" is short for "configuration".
Think of the config object as an initialization object; not as a runtime transport/storage object.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all.
I was relating config with all others without any reason.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just read that context is also read only (deploy time constant). How it is scope object then???

Thanks.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The context object is not read-only.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
The context object is not read-only.


Please look at page 160 of HFS&J.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:


Please look at page 160 of HFS&J.


I dont own the book but Ben is right - Context is *not* read-only.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:

Please look at page 160 of HFS&J.


Is there anything like context is readonly? I dont think so.
Could you please paste the phrase here.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adeel Ansari:


Is there anything like context is readonly? I dont think so.
Could you please paste the phrase here.


okay,
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
Look in ServletContext or ServletConfig and you will find a getter (getInitParameter()) but you will not find setter. There is no setInitParameter().


But it doesn't mean that ServletContext is readobnly. We have the method setAttribute() in there. And regarding setInitParameter() ofcourse we dont have. How can we set initial parameters afterwards take it logically. But obviously we can get.

thanks.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are confusing Attributes with Parameters

Yes as per the book and in reality context parameters (also just like request parameters) cannot be changed via code. They are configured in DD.

Attributes can be get and set whereas parameters( Both request and context) can only be get via getParameter() and getInitParameter().
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot everybody.

If this is the case then why we are not allowed to set attributes in config just like context.

Thanks.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
If this is the case then why we are not allowed to set attributes in config just like context.


Because its read-only.
Well, forget about setting attributes in config.
Tell me how would I get attributes from config just like context.

Do we have getAttribute() method in ServletConfig?

We have neither. Actually, there are nothing like attributes in there. It only has initial parameters, which are defined into web.xml, and those are read-only.

After going through the complete discussion. I would like to say that please be rational and ask meaningful questions. Ask when you really want to learn.
[ November 10, 2005: Message edited by: Adeel Ansari ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adeel Ansari:


Because its read-only.
Well, forget about setting attributes in config.
Tell me how would I get attributes from config just like context.

Do we have getAttribute() method in ServletConfig?

We have neither. Actually, there are nothing like attributes in there. It only has initial parameters, which are defined into web.xml, and those are read-only.

After going through the complete discussion. I would like to say that please be rational and ask meaningful questions. Ask when you really want to learn.

[ November 10, 2005: Message edited by: Adeel Ansari ]


I am very sorry for this question.

Actually, by mistake, I looked ServletContext API and moreever never looked setAttribute() method (only looked getAttribute() method) so I thought this question...we have get method then why not we have set method??

But now it is clear.

1] Config doesn't have attributes...
2] Context has attributes and we can get and set them at run time...
3] Both has parameters but we can only set them at deploy time...


It is height of demand but please let me ask one more...

Why we don't have attributes in config like context??
Thanks.
[ November 10, 2005: Message edited by: rathi ji ]
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why we don't have attributes in config like context??

What would you use them for?

Remember, there is only one ServletContext obbject per web application (or per JVM in a cluster). So it makes sense for a servlet to be able to set attributes on the context object so they may be retrieved by other servlets.

However, there is one ServletConfig object for every servlet. If a servlet were able to set an attribute on the config object... the only servlet that could retrieve it would be the same servlet. There is simply no reason for it. Hope that helps.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Bourdeaux:
Why we don't have attributes in config like context??

What would you use them for?

Remember, there is only one ServletContext obbject per web application (or per JVM in a cluster). So it makes sense for a servlet to be able to set attributes on the context object so they may be retrieved by other servlets.

However, there is one ServletConfig object for every servlet. If a servlet were able to set an attribute on the config object... the only servlet that could retrieve it would be the same servlet. There is simply no reason for it. Hope that helps.



Superb explanation.
Thank you very much Paul.

If I ask that why we have attributes in request then one answer may be becasue we can set something in it and forward that request to some other resource, now this resource can get that...am I right?

Thanks.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, sounds like you got it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic