1) A parameter is defined in a <context-param> element of the deployment descriptor for a web application. Which of the following statements
is correct? Select 1 correct option.
A. It is accessible to all the servlets of the webapp. B. It is accessible to all the servlets of all the webapps of the container. C. It is accessible only to the servlet it is defined for. D. It is accessible only in the init() method of the servlet. E. None of the above is correct.
Charles Lyons replied In fact both are correct
Now as per my reference material
Question Discuss the behaviour of servlet context in distributed environment?
1. Each web application has one and only one ServletContext instance on each JVM.
2. Servlet Context attributes set on one JVM are not visible on another JVM.
As the context attributes are local to the JVM in which they are declared,this prevents the ServletContext attributes to be shared in distributed environment,When information needs to be shared between the servlets running in distributed environment ,the information should be placed into session
Point 4 says the Initialization parameters are available in all JVM's so that way only answer to the question comes out to be option 1 No way it can be both
Please clarify if I am wrong
Joined: Mar 27, 2003
I would have preferred it if this had continued in the same thread, but never mind...
Yes, I see how you are looking at this question now. Indeed, since context initialisation parameters are immutable once set in the deployment descriptor, then assuming the same DD is deployed to each server, the same set of parameters will be available to each application.
So provided they are exactly the same application deployed on different machines, then the context parameters will be available to every component of each instance of the application.
But, and this was what I was implying, the context parameters are never migrated between different JVMs... Aleksander's comment was "so Servlets in second JVM will not have access to context param of ServletContext in first JVM". If we're talking about objects this is correct - servlets in one application can't invoke methods on context parameters in a different application. This is of course a different story to session attributes, whose instances can effectively be cloned between the two applications and any change in state between invocations are persisted.
However, assuming the applications are deployed in the same way, they should each contain the same set of context parameters. Since these are immutable, they contain the same data and so each application has access to identically named parameters with identical values. But these are not the same parameters in the object sense... method invocations on either occur quite separately.
Of course, a change in the deployment descriptor would also lead to each application having access to different parameters (and in theory this could happen if you had different Deployer personnel for different Web servers). Indeed, what would happen if your DD contained a host name and configuration for a mail server? This would most likely be different on each machine the application was deployed on, so even though the same parameter names appear, they have different values. But other applications can't 'see' the different values - they can only determine their own. So the context parameters are available in all applications, but they have different values.
Again, this is an esoteric argument of words, which is why both answers can be seen to be correct, depending on how you want to interpret the conditions, and what exact restrictions are given. In my opinion the question is quite unclear. Indeed, I could argue that in fact option D ("It is accessible only in the init() method of the servlet") is true if we choose never to save the ServletConfig object in the servlet... no ServletConfig object means no ServletContext object is available for use in any other methods of the servlet, so no access to context parameters elsewhere! This would have been better worded as "It is only ever accessible in the init() method of the servlet" which is then clearly false.
That might have made things more confusing, I don't know... [ May 11, 2006: Message edited by: Charles Lyons ]
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / AmazonAmazon UK )