0

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?

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • So you are asking whether `getSevletConfig()` will always create a new instance of `ServletConfig` if there are no init params ? – Svetlin Zarev Mar 26 '19 at 12:28
  • Well, if Container does not create - then yes, that is the question, and why we may need that ServletConfig object in that case.. but who knows, maybe container creates that object at context startup. – Giorgi Tsiklauri Mar 26 '19 at 12:30
  • At least tomcat creates only one such object per servlet: org/apache/catalina/core/StandardWrapper.java:124 (tomcat 8.5) – Svetlin Zarev Mar 26 '19 at 12:34

1 Answers1

1

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.

Pino
  • 7,468
  • 6
  • 50
  • 69