Context Init parameters work just like Servlet init parameters, except context parameters are available to the entire webapp, not just a single servlet. So that means any servlet and JSP in the app automatically has access to the Context Init Parameters, so we don’t have to worry about configuring the Deployment Descriptor for every servlet, and when the value changes, you only have to change it one place!.
Difference No#1:- Deployment Descriptor
Context init parameter
Within the < web-app > element but NOT within a specific < servlet > element
< web-app >
< context-param >
< param-name >Country< /param-name >
< param-value >India param-value="">
< /context-param >
< !-- other stuff including servlet declarations -- >
< /web-app >
Servlet init parameters
Within the < servlet > element for each specific servlet
< servlet>
< servlet-name >ServletController< /servlet-name >
< servlet-class >com.examp.ServletController< /servlet-class >
< init-param >
< param-name >Country< /param-name >
< param-value >India< /param-value >
< /init-param >
< /servlet >
Difference No#2 :- Servlet code
Context init parameter
getServletContext().getInitParameter(“foo”);
Servlet init parameters
getServletConfig().getInitParameter(“foo”);
Difference No#3:- Availability
Context init parameter
To any servlets and JSPs that are part of this web app.
Servlet init parameters
To only the servlet for which thewas configured. Although the servlet can choose to make it more widely available by storing it in an attribute.
Difference No#1:- Deployment Descriptor
Context init parameter
Within the < web-app > element but NOT within a specific < servlet > element
< web-app >
< context-param >
< param-name >Country< /param-name >
< param-value >India param-value="">
< /context-param >
< !-- other stuff including servlet declarations -- >
< /web-app >
Servlet init parameters
Within the < servlet > element for each specific servlet
< servlet>
< servlet-name >ServletController< /servlet-name >
< servlet-class >com.examp.ServletController< /servlet-class >
< init-param >
< param-name >Country< /param-name >
< param-value >India< /param-value >
< /init-param >
< /servlet >
Difference No#2 :- Servlet code
Context init parameter
getServletContext().getInitParameter(“foo”);
Servlet init parameters
getServletConfig().getInitParameter(“foo”);
Difference No#3:- Availability
Context init parameter
To any servlets and JSPs that are part of this web app.
Servlet init parameters
To only the servlet for which the
This post first appeared on Every Day Of Your Life Is A Page Of Your History, please read the originial post: here