Init-param and context-param

What is the difference between <init-param> and <context-param> !?

+79
java servlets
08 Feb '15 at 10:25
source share
4 answers

<init-param> and <context-param> are static parameters that are stored in the web.xml file. If you have data that does not change often, you can save it in one of them.

If you want to store certain data that is limited only to specific areas of the servlet , you can use <init-param> . Everything you declare inside <init-param> is only available for this particular servlet. init-param is declared inside the <servlet> .

 <servlet> <display-name>HelloWorldServlet</display-name> <servlet-name>HelloWorldServlet</servlet-name> <init-param> <param-name>Greetings</param-name> <param-value>Hello</param-value> </init-param> </servlet> 

and you can access these parameters in the servlet as follows:

 out.println(getInitParameter("Greetings")); 

If you want to store data that is common to the entire application , and if it does not change often, you can use <context-param> instead of the servletContext.setAttribute() method of the application context. For more information on using <context-param> VS servletContext.setAttribute() see this question. context-param are declared in the web-app tag. You can declare and access <context-param> as follows

 <web-app> <context-param> <param-name>Country</param-name> <param-value>India</param-value> </context-param> <context-param> <param-name>Age</param-name> <param-value>24</param-value> </context-param> </web-app> 

Use in the application in either JSP or Servlet

 getServletContext().getInitParameter("Country"); getServletContext().getInitParameter("Age"); 
+110
Feb 08 '15 at 11:19
source share
β€” -

Consider the definition below in web.xml

 <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>TestServlet</servlet-class> <init-param> <param-name>myprop</param-name> <param-value>value</param-value> </init-param> </servlet> 

You can see that init-param is defined inside the servlet element. This means that it is available only to the servlet under the declaration, and not to other parts of the web application. If you want this parameter to be available to other parts of the application, say, JSP, this should be explicitly passed to JSP. For example, passed as request.setAttribute (). It is very inefficient and difficult to code.

So, if you want to access global values ​​from anywhere in the application without explicitly passing these values, you need to use the Init context parameters.

Consider the following definition in web.xml

  <web-app> <context-param> <param-name>myprop</param-name> <param-value>value</param-value> </context-param> </web-app> 

This context parameter is available to all parts of the web application and can be retrieved from the Context object. For example, getServletContext (). GetInitParameter ("dbname");

From the JSP, you can access the context parameter using an implicit application object. For example, application.getAttribute ("dbname");

+8
Feb 08 '15 at 10:43
source share
 <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:/META-INF/PersistenceContext.xml </param-value> </context-param> 

I initialized my PersistenceContext.xml inside <context-param> , because all my servlets will interact with the database as part of MVC.

Powerver,

 <servlet> <servlet-name>jersey-servlet</servlet-name> <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:ApplicationContext.xml </param-value> </init-param> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.organisation.project.rest</param-value> </init-param> </servlet> 

in the above code, I set up the hosiery and ApplicationContext.xml just for the rest layer. For this I use </init-param>

+6
Feb 08 '15 at 10:52
source share

<init-param> will be used if you want to initialize some parameter for a specific servlet. When the request comes to the servlet first, its init method will be called then doGet/doPost , whereas if you want to initialize some variable for the whole application, you will need to use <context-param> . Each servlet will have access to a context variable.

+5
Feb 08 '15 at 10:37
source share



All Articles