Bug 56223

Summary: ServletContext#setInitParameter(..) does not throw IllegalStateException after it's been initialized
Product: Tomcat 8 Reporter: Sotirios Delimanolis <sotodel_89>
Component: Specification APIsAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 8.0.3   
Target Milestone: ----   
Hardware: PC   
OS: All   
Attachments: .war with single Servlet

Description Sotirios Delimanolis 2014-03-06 02:15:48 UTC
Created attachment 31369 [details]
.war with single Servlet

The Java EE javadoc for ServletContext#setInitParameter(String, String), here: http://docs.oracle.com/javaee/7/api/javax/servlet/ServletContext.html#setInitParameter(java.lang.String, java.lang.String), states that the method should throw an IllegalStateException if the ServletContext has already been initialized.

Once a Servlet is ready to handle requests, the ServletContext must have been initialized. However, no exception is thrown if you try to invoke the method in an HttpServlet's doGet(..).

Steps to reproduce:

1) Add an HttpServlet that invokes the method in its doGet(..) method

@WebServlet(urlPatterns = "/init")
public class InitParameterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ServletContext context = getServletContext();
		context.setInitParameter("init-param", "value");

	}
}

2) Send an HTTP GET request to (the appropriate) 'http://localhost:8080/context/init`.

Actual results: No exception is thrown. Server sends back 200 OK.

Expected results: An exception should have been thrown. Server should have sent back 500 status code.

Tomcat 8.0.3 (and 7.0.42) and Windows 7

Additional information: You can also try calling, for example, the addFilter(..) method which will fail stating that the ServletContext is already initialized.
Comment 1 Mark Thomas 2014-03-06 16:21:38 UTC
Thanks for the report.

This has been fixed in 7.0.x for 7.0.53 onwards and in 8.0.x for 8.0.4 onwards.