| Author |
Declaring ServletContext
|
Damien Clarke
Greenhorn
Joined: Mar 10, 2010
Posts: 6
|
|
Hi all,
One thing from Servlet spec that I don't fully understand - maybe you guys can shed some light?
Section 4.4 Configuration Methods
"If the ServletContext passed to the ServletContextListener’s
contextInitialized method was neither declared in web.xml or webfragment.
xml nor annotated with @WebListener then an
UnsupportedOperationException MUST be thrown for all the methods defined
for programmatic configuration of servlets, filters and listeners."
Where/how is the ServletContext declared? It was my understanding that this is created/provided by the container?
Can anyone give an example of it declared in web.xml as suggested above?
Thanks
|
 |
Yasir Karim
Greenhorn
Joined: Jul 21, 2011
Posts: 17
|
|
Good question. I have the same and will appreciate if someone can answer this.
As I understand, there is a single ServletContext per web-app in a single JVM. But I don"t understand that is meant by the following:
"If the ServletContext passed to the ServletContextListener’s
contextInitialized method was neither declared in web.xml or webfragment.
xml nor annotated with @WebListener then an
UnsupportedOperationException MUST be thrown for all the methods defined
for programmatic configuration of servlets, filters and listeners."
|
I see the truth in the code.
|
 |
Christian Ludt
Greenhorn
Joined: Apr 07, 2009
Posts: 9
|
|
I am not able to make heads or tails of this sentence either. The ServletContext is created by the container, hence I don't see how it can be declared or annotated.
Correct me if I am wrong, I do not think there is any method, that can be / is supposed to be called by application code, that requires a ServletContext as parameter - where should the application code get it from? ServletContext is always passed from the container to the application.
I believe the only one who can shed some light is the author.
|
 |
Christian Ludt
Greenhorn
Joined: Apr 07, 2009
Posts: 9
|
|
I gave it a second thought and came to the conclusion that
neither declared in web.xml or webfragment.xml nor annotated with @WebListener
refers to the ServletContextListener.
I opened a JIRA ticket for that (http://java.net/jira/browse/SERVLET_SPEC-35).
But as also mentioned in the ticket, I don't understand, why and when an UnsupportedOperationException must be thrown. Must it be thrown when that ServletContextListener is added via ServletContext#addListener? What sense would it make to throw an exception here? If that listener had been declared in web.xml or webfragment.xml or annotated, there would be no need to add it programmatically in the first place. Any thoughts on that?
|
 |
Dieter Quickfend
Ranch Hand
Joined: Aug 06, 2010
Posts: 280
|
|
|
I think it means that it should throw unsupportedoperationexception if you should create a servletcontextlistener instance in some code after startup and try to add a listener programmatically through its contextInitialized method.
|
Oracle Certified Professional Java Programmer
|
 |
 |
|
|
subject: Declaring ServletContext
|
|
|