File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes ServletContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "ServletContext" Watch "ServletContext" New topic
Author

ServletContext

vasu maj
Ranch Hand

Joined: Jul 12, 2001
Posts: 395
This is from Manning's Study kit.
Initialization parameters are specified in the deployment descriptor and cannot be added programmatically. You can add an attribute to servletContext by using setAttribute() method though.
What is the difference between an init parameter and an attribute?

Vasu


What a wonderful world!
Rick Portugal
Ranch Hand

Joined: Dec 17, 2002
Posts: 243
I wondered the same thing. Here's the way I've got it figured.
Unlike an attribute, a parameter can be passed in. Also, a parameter must be a String, but an attribute can be any object. That's my theory and I'm sticking to it.
It isn't very clear from the text, which is unfortunate because it seems like a key point. There is a reference to this on page 243, but it doesn't seem to directly answer your question.


IBM 286, SCJP, SCWCD, EIEIO
vasu maj
Ranch Hand

Joined: Jul 12, 2001
Posts: 395
I figured it out. parameters are the ones that are defined in the web.xml file. Attributes are objects that are added within the code.
Thanks,
vasu
Tejas Tambe
Ranch Hand

Joined: Oct 08, 2001
Posts: 47
Hello Vasu,
As per my understanding, the setAttribute method is used to share the data or variables amongst other servlets or JSPs. The initializing parameters however are those whose values need not be changed often, or will remain constant such as a ODBC connection string and will be meant for a particular Servlet.
The initializing parameters have a life span depending upon the Containers used for sharing, but the lifespan of initializing parameters will
depend upon the lifespan of the Servlet.
Please correct me if I am wrong.
Regards,
Tejas Y. Tambe
SCJP2
Dharmin Desai
Ranch Hand

Joined: Feb 28, 2002
Posts: 81
Dear Vasu, Rick and Tejas
Please note following points:
1. Attributes r Context values, Which r shared among the context (context mean to say an app root - Each servlet which r from the same app)
Whereas Initialization parameters r for an Individual servlet.
2. Attributes can be set/get through ServletContext Object.
Whereas Initialization parameters can only GET through ServletConfig Object or ServletConfig object.
3. Attributes and Initialization Parameters both can be set in XML file But Attributes can be set programmatically also. => setAttribute(java.lang.String name, java.lang.Object object) method of ServletContext class.
4. In XML file, Context Attribute uses, <context-param> ... </context-param> tags
Whereas Intialization parameters uses <init-param> .... </init-param> tags
5. getAttribute() method returns an OBJECT where as getInitParameter() method returns String.
So it is obvious that, any object can be set as an attribute whereas an init parameter should only an object of String class.
=> getInitParameter(java.lang.string) method is at SevletContext and also at ServletConfig interface, both methods seems same and returning Servlet Intialization parameter for an individual servlet as a String object.
Hope i m clear enough, If i m missing anywhere please reply.
With best regards, Dharmin


SCJP2 (93%),SCWCD(88%)<br />-------------------------------<br />Never under estimate yr self, just represent yr profile in proper manner.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Dharmin:
1. Attributes r Context values, Which r shared among the context (context mean to say an app root - Each servlet which r from the same app) Whereas Initialization parameters r for an Individual servlet.
I don't think scope has anything to do with the difference between init parameters and attributes. Initialization parameters exist both in application scope and servlet scope. Attributes exist both in application scope and servlet scope -- note that in servlet scope they are also known as "instance variables"
2. Attributes can be set/get through ServletContext Object. Whereas Initialization parameters can only GET through ServletConfig Object or ServletConfig object.
This is a corollary to the fundamental difference between the two --- both application and servlet initialization parameters ultimately come from the web.xml deployment descriptor. Which is read only. So initialization parameters have to be read only too. Another consequence is that, since the value is an XML CDATA, init parameters are represented by Strings as you mention under 5.
3. Attributes and Initialization Parameters both can be set in XML file But Attributes can be set programmatically also.
You can only set them programmatically.
4. In XML file, Context Attribute uses, <context-param> ... </context-param> tags Whereas Intialization parameters uses <init-param> .... </init-param> tags
Typo: you meant "context initialization parameter", I presume.
=> getInitParameter(java.lang.string) method is at SevletContext and also at ServletConfig interface, both methods seems same and returning Servlet Intialization parameter for an individual servlet as a String object.
They are different: they return application and servlet initialization parameters respectively.
- Peter
Dominic Paquette
Ranch Hand

Joined: Dec 13, 2002
Posts: 64
Hi, here is how I understand it:
ServletContext.getInitParameter(String)
gets the value specified in the deployment descriptor:
<web-app>
<context-param>
<param-name>yourParamName</param-name>
<param-value>someValue</param-value>
</context-param>
<servlet>
...
</web-app>
and ServletConfig.getInitParameter(String) will return the parameters specified, again in the deployment descriptor but under the <servlet> tag like this:
<web-app>
<context-param>
<param-name>yourParamName</param-name>
<param-value>someValue</param-value>
</context-param>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
<init-param>
<param-name>yourParamName</param-name>
<param-value>someValue</param-value>
</init-param>
</servlet>
...
</web-app>

As for the getAttribute(String) and setAttribute(String, Object), they are defined in ServletRequest, HttpSession and ServletContext and are used to set/get an object in the corresponding scope(request, session or application).
What do you think!
Dominic
vasu maj
Ranch Hand

Joined: Jul 12, 2001
Posts: 395
You are right dominic.
Dharmin Desai
Ranch Hand

Joined: Feb 28, 2002
Posts: 81
Dear peter and dominic,
U both found much clear about the things.
Thanks to both.
Sincerely Dharmin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletContext