Search code examples
javaservletsservletconfig

Does Servlet Container create ServletConfig object if there are no init-params?


I know, that when Servlet Container instantiates Servlet object, it also creates respective ServletConfig object, where it stored all init-params for that servlet (which we can later fetch via servlet by .getServletConfig().getInitParameter(..)

My question is what happens when we don't have any init-param in the servlet element? does the container still create ServletConfig with no init-params? (.getSevletConfig() does not return null, so I'm not sure whether ServletConfig is created in runtime when invoking this method, or container still creates that object with no init-params) or in this case, container does not create ServletConfig object? if it does, why we may need that ServletConfig object?


Solution

  • The ServletConfig does not contain only init params but also a reference to the ServletContext and the servlet name, so it makes sense even if no init param has been specified.