DD elements <context-param>
and <init-param>
both can be retrieved by the getInitParameter()
method, in the servlet code.
Now the question is, how does it differentiate <context-param>
and <init-param>
?
Context Parameter is a variable that is replaced by a predefined or calculated value during monitoring or load testing. Value could be assigned directly, taken from a file, calculated in a script, or be a result of the execution of another task.
The context-param element, subelement of web-app, is used to define the initialization parameter in the application scope. The param-name and param-value are the sub-elements of the context-param. The param-name element defines parameter name and and param-value defines its value.
Context Initialization parameters are the parameter name and value pairs that you can specify in the deployment descriptor file (the web. xml file). Here you can specify the parameters that will be accessible to all the servlets in the web application.
In a spring web application, contextConfigLocation context param gives the location of the root context. Your config is strange, for a spring-mvc application, because by default, servletname-servlet. xml (where servletname is the name of a DispatcherServlet servlet) is the child application context for the servlet.
Servlet init parameters are for a single servlet only. Nothing outside that servlet can access that. It is declared inside the <servlet>
tag of Deployment Descriptor, on the other hand context init parameter is for the entire web application. Any servlet or JSP in that web application can access context init parameter. Context parameters are declared in a tag <context-param>
directly inside the <web-app>
tag.
The methods for accessing context init parameter is
getServletContext().getInitParameter("name");
whereas the method for accessing servlet init parameter is
getServletConfig().getInitParameter("name");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With