This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring ServletContext

 
Damien Clarke
Greenhorn
Posts: 8
  • 0
  • 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
 
Yasir Karim
Greenhorn
Posts: 17
Java
  • 0
  • 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."
 
Christian Ludt
Greenhorn
Posts: 11
  • 0
  • 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
  • 0
  • 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?
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • 0
  • 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