• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Declaring ServletContext  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!