File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes ServletContext.setInitParameter does not throw IllegalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "ServletContext.setInitParameter does not throw IllegalStateException" Watch "ServletContext.setInitParameter does not throw IllegalStateException" New topic

ServletContext.setInitParameter does not throw IllegalStateException

Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 146

Hi all.
Considering Oracle's documentation for ServletContext.setInitParameter method, there is the following assertion when describing the exceptions that could be thrown when setInitParameter is invoked:
IllegalStateException - if this ServletContext has already been initialized

To test this, I've created a context parameter at DD (Web Deployment Descriptor - which should be initialized when the application is started) .
So I've tried to change the attribute value expecting for an exception (IllegalStateException in such case).

I'd like to know why this exception isn't thrown?

Context parameter defined at DD:

And the servlet's code that tries to change the context parameter through setInitParameter.

Thanks in advance.

Adolfo Eloy
Software Developer
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924

Hello Adolfo.

you have misinterpreted the documenation. the docs say that the IllegalStateException occurs when the ServletContext has already been initialized. it DOES NOT say that if you try to override the context parameters. setting the parameters and initialization of ServletContext are 2 different things, although when ServletContext is initiazlized(at deployment time) it is given references to context parameters name/value pairs but they ain't the same.

Regarding your second query the doc say that setInitParameter will return false if you try to set a parameter whose value is already set earlier. so in your case the name/value pairs will not be changed by the method call.

Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper

Joined: Apr 07, 2010
Posts: 2102

The reason why this IllegalStateException can be thrown is explained in this thread.

I agree. Here's the link:
subject: ServletContext.setInitParameter does not throw IllegalStateException
It's not a secret anymore!