2015-02-08 70 views
51

<init-param><context-param>!有什么区别?init-param和context-param

+0

@RC。这个问题没有 - 在哪里讨论这两个参数之间的区别..它只是关于在两个上下文中加载的属性。 – 2015-02-08 10:33:26

+0

我停在标题,我的坏。 – 2015-02-08 10:36:31

回答

76

<init-param><context-param>是存储在web.xml文件中的静态参数。如果您有任何不经常更改的数据,则可以将其存储在其中一个中。

如果你想存储它仅局限于特定的servlet范围特定的数据,那么你可以使用<init-param> .Anything声明里面<init-param>是只能针对特定servlet.The访问的init-PARAM是在<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> 

,你可以在servlet访问这些参数如下:

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

如果你想存储数据,是很常见的整个应用程序,如果不经常更改你可以使用<context-param>而不是servletContext.setAttribute()应用上下文的方法。欲了解更多关于使用<context-param> VS ServletContext.setAttribute()看看这questioncontext-param在标签web-app下声明。 可以声明并访问<context-param>如下的应用

<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> 

用法无论是在JSP或Servlet的

getServletContext().getInitParameter("Country"); 
getServletContext().getInitParameter("Age"); 
4

<init-param>将被使用,如果你想初始化一个特定的servlet参数。当请求首先来到servlet时,其init方法将被调用,然后调用doGet/doPost,而如果要为整个应用程序初始化一些变量,则需要使用<context-param>。每个servlet都可以访问上下文变量。

5

考虑在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> 

下面的定义可以看到的init-param为一个servlet元素中定义。这意味着它仅适用于声明中的servlet,而不适用于Web应用程序的其他部分。如果你希望这个参数对应用程序的其他部分可用,比如一个JSP,这需要显式地传递给JSP。例如作为request.setAttribute()传递。这是非常低效且难以编码的。

因此,如果您希望从应用程序的任何位置访问全局值而不显式传递这些值,则需要使用Context Init参数。

考虑在web.xml

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

以下定义这个上下文参数是可用的web应用程序的所有部分并且它可以从语境对象进行检索。例如,getServletContext()。getInitParameter(“dbname”);

从JSP可以使用应用程序隐式对象访问上下文参数。例如,application.getAttribute(“dbname”);

5
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     classpath*:/META-INF/PersistenceContext.xml 
    </param-value> 
</context-param> 

我已经内<context-param>初始化我PersistenceContext.xml,因为我的所有servlet将与MVC框架数据库进行交互。

Howerver,

<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> 
在上述代码

,我配置的球衣和ApplicationContext.xml只休息层。对于我正在使用的</init-param>