Java 初始化参数和上下文参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28392888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
init-param and context-param
提问by mina
What is the difference between <init-param>
and <context-param>
!?
<init-param>
和<context-param>
!和有什么不一样?
采纳答案by Sai Upadhyayula
<init-param>
and <context-param>
are static parameters which are stored in web.xml file. If you have any data which doesn't change frequently you can store it in one of them.
<init-param>
和<context-param>
是存储在 web.xml 文件中的静态参数。如果您有任何不经常更改的数据,您可以将其存储在其中之一中。
If you want to store particular data which is confined to a particular servlet scope, then you can use <init-param>
.Anything you declare inside <init-param>
is only accessible only for that particular servlet.The init-paramis declared inside the <servlet>
tag.
如果您想存储被限制在特定 servlet 范围内的特定数据,那么您可以使用<init-param>
.Anything 您在内部声明的<init-param>
内容仅可被该特定 servlet访问。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>
and you can access those parameters in the servlet as follows:
您可以在 servlet 中访问这些参数,如下所示:
out.println(getInitParameter("Greetings"));
If you want to store data which is common for whole applicationand if it doesn't change frequently you can use <context-param>
instead of servletContext.setAttribute()
method of the application context. For more information regarding usage of <context-param>
VS ServletContext.setAttribute()
have a look at this question. context-paramare declared under the tag web-app
.
You can declare and access the <context-param>
as follows
如果您想存储整个应用程序通用的数据并且如果它不经常更改,则可以使用<context-param>
代替servletContext.setAttribute()
应用程序上下文的方法。有关<context-param>
VS使用的更多信息,请ServletContext.setAttribute()
查看此问题。上下文参数在标签下声明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>
Usage in the application either in a JSP or Servlet
在 JSP 或 Servlet 中的应用程序中的使用
getServletContext().getInitParameter("Country");
getServletContext().getInitParameter("Age");
回答by Adeel Ahmad
<init-param>
will be used if you want to initialize some parameter for a particular servlet. When request come to servlet first its init
method will be called then doGet/doPost
whereas if you want to initialize some variable for whole application you will need to use <context-param>
. Every servlet will have access to the context variable.
<init-param>
如果您想为特定的 servlet 初始化一些参数,将使用。当请求首先到达 servlet 时,它的init
方法将被调用,doGet/doPost
而如果你想为整个应用程序初始化一些变量,你将需要使用<context-param>
. 每个 servlet 都可以访问上下文变量。
回答by SMA
Consider the below definition in web.xml
考虑 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 a servlet element. This means it is only available to the servlet under 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 a JSP this needs to be explicitly passed to the JSP. For instance passed as request.setAttribute(). This is highly inefficient and difficult to code.
您可以看到 init-param 是在 servlet 元素中定义的。这意味着它仅可用于声明下的 servlet,而不能用于 Web 应用程序的其他部分。如果您希望此参数可用于应用程序的其他部分,例如 JSP,则需要将其显式传递给 JSP。例如作为 request.setAttribute() 传递。这是非常低效且难以编码的。
So if you want to get access to global values from anywhere within the application without explicitly passing those values, you need to use Context Init parameters.
因此,如果您想从应用程序内的任何位置访问全局值而不显式传递这些值,则需要使用 Context Init 参数。
Consider the following definition in web.xml
考虑 web.xml 中的以下定义
<web-app>
<context-param>
<param-name>myprop</param-name>
<param-value>value</param-value>
</context-param>
</web-app>
This context param is available to all parts of the web application and it can be retrieved from the Context object. For instance, getServletContext().getInitParameter(“dbname”);
此上下文参数可用于 Web 应用程序的所有部分,并且可以从 Context 对象中检索。例如,getServletContext().getInitParameter(“dbname”);
From a JSP you can access the context parameter using the application implicit object. For example, application.getAttribute(“dbname”);
在 JSP 中,您可以使用应用程序隐式对象访问上下文参数。例如,application.getAttribute(“dbname”);
回答by Ankur Piyush
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/META-INF/PersistenceContext.xml
</param-value>
</context-param>
I have initialized my PersistenceContext.xml
within <context-param>
because all my servlets will be interacting with database in MVC framework.
我已经初始化了我的PersistenceContext.xml
内部,<context-param>
因为我所有的 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>
in the aforementioned code, I am configuring jersey and the ApplicationContext.xml
only to rest layer. For the same I am using </init-param>
在上述代码中,我正在配置球衣和ApplicationContext.xml
唯一的休息层。对于我正在使用的相同</init-param>
回答by Basil Bourque
What is the difference between
<init-param>
and<context-param>
!?
<init-param>
和<context-param>
!和有什么不一样?
Single servletversus multiple servlets.
单个 servlet与多个 servlet。
Other Answers give details, but here is the summary:
其他答案提供了详细信息,但这里是摘要:
A web app, that is, a “context”, is made up of one or more servlets.
一个 Web 应用程序,即“上下文”,由一个或多个 servlet 组成。
<init-param>
defines a value available to a single specific servletwithin a context.<context-param>
defines a value available to all the servletswithin a context.
<init-param>
定义上下文中单个特定 servlet可用的值。<context-param>
定义了一个可用于上下文中所有 servlet的值。