This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes why config is read only??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "why config is read only???" Watch "why config is read only???" New topic
Author

why config is read only???

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: May 24, 2004
Posts: 783
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.


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

But one of my friend told me that config is not scope object


He is correct.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Nov 15, 2004
Posts: 1026
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.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
subho saha
Ranch Hand

Joined: Jun 15, 2005
Posts: 57
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jan 10, 2002
Posts: 61449
    
  67

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

Joined: Dec 11, 2004
Posts: 13410

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.


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

Joined: Oct 11, 2004
Posts: 3830
Thanks all.
I was relating config with all others without any reason.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
I just read that context is also read only (deploy time constant). How it is scope object then???

Thanks.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The context object is not read-only.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Ben Souther:
The context object is not read-only.


Please look at page 160 of HFS&J.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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.


Groovy
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Nov 15, 2004
Posts: 1026
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: May 24, 2004
Posts: 783
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: May 24, 2004
Posts: 783
Yep, sounds like you got it!
 
Consider Paul's rocket mass heater.
 
subject: why config is read only???