aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes setInitParameter() 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 "setInitParameter()" Watch "setInitParameter()" New topic
Author

setInitParameter()

Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
I'm a bit confused with regards to the new setInitParameter(java.lang.String name, java.lang.String value) method of ServletContext.

Looking at the API, it says that this method "throws an IllegalStateException if this ServletContext has already been initialized". Now won't the ServletContext always be initialised before any Servlet is initialised? Does this mean you can't call setInitParameter() programmatically from within a Servlet??

OCA, OCP Java 6
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

Hi,

Does this mean you can't call setInitParameter() programmatically from within a Servlet??

Yes, you can't (but Tomcat allows you to do it inside a Servlet...)

This method was added to the Servlet 3.0 in relation with the programmable interface:
4.4 Configuration methods
The following methods are added to ServletContext since Servlet 3.0 to enable programmatic definition of servlets, filters and the url pattern that they map to. These methods can only be called during the initialization of the application either from the contexInitialized method of a ServletContextListener implementation or from the onStartup method of a ServletContainerInitializer implementation. In addition to adding Servlets and Filters, one can also look up an instance of a Registration object corresponding to a Servlet or Filter or a map of all the Registration objects for the Servlets or Filters. 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

4.4.1 Programmatically adding and configuring Servlets
The ability to programmatically add a servlet to a context is useful for framework developers. For example a framework could declare a controller servlet using this method. The return value of this method is a ServletRegistration or a ServletRegistration.Dynamic object which further allows you to setup the other parameters of the servlet like init-params, url-mappings etc. There are three overloaded versions of the method as described below.
...


Regards,
Frits
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
I see... very clear explanation in the documentation, thanks a lot for posting that, Fritz.
Jaime Hablutzel
Greenhorn

Joined: Dec 25, 2009
Posts: 6
I have just read the relevant parts in "Java™ Servlet Specification Version 3.0 Rev a" and I can't see the definition of the method "setInitParameter()" is this really a standard one?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

Just check the API of the ServletContext here.

Regards,
Frits
Jaime Hablutzel
Greenhorn

Joined: Dec 25, 2009
Posts: 6
So the method isn't standard? as it is not in the spec but directly in code?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

It is a standard method.

Note that specifications normally do not cover the full API.
If you go to the download page of the JSR-000315 Java Servlet 3.0 page you will find both the specifications and the API of Servlet 3.0.

Regards,
Frits
 
wood burning stoves
 
subject: setInitParameter()