java b/w <context-param> 和 <init-param> 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2069902/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:14:35  来源:igfitidea点击:

Difference b/w <context-param> and <init-param>

javaservletsdeployment-descriptorinit-parameters

提问by giri

DD elements <context-param>and <init-param>both can be retrieved by the getInitParameter()method, in the servlet code.

可以通过servlet 代码中的方法检索DD 元素<context-param><init-param>两者getInitParameter()

Now the question is, how does it differentiate <context-param>and <init-param>?

现在的问题是,它如何区分<context-param><init-param>

回答by Adeel Ansari

Servlet init parametersare 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 parameteris for the entire web application. Any servlet or JSP in that web application can access context init parameter. Context parametersare declared in a tag <context-param>directly inside the <web-app>tag.

Servlet init 参数仅适用于单个 servlet。servlet 之外的任何东西都不能访问它。它在<servlet>Deployment Descriptor的标签内声明,另一方面context init 参数是针对整个 web 应用程序的。该 Web 应用程序中的任何 servlet 或 JSP 都可以访问上下文初始化参数上下文参数<context-param>直接在<web-app>标签内部的标签中声明。

The methods for accessing context init parameteris

访问上下文初始化参数的方法是

getServletContext().getInitParameter("name"); 

whereas the method for accessing servlet init parameteris

而访问servlet init参数的方法是

getServletConfig().getInitParameter("name");

回答by Gladwin Burboz

As explained by Adeel Ansari, here, it depends on what object are you invoking the method getInitParameter()in the servlet code.

正如Adeel Ansari所解释的,这里,这取决于您调用getInitParameter()servlet 代码中的方法的对象是什么。

All servlets extends from and hence are instance of GenericServlet.

所有 servlet 都从GenericServlet.

.

.

DD elements <context-param>can be retrieved by:

<context-param>可以通过以下方式检索DD 元素:

ServletContext context = this.getServletContext();
String paramValue = context.getInitParamter("paramName");

.

.

DD elements <init-param>both can be retrieved by:

DD 元素<init-param>都可以通过以下方式检索:

ServletConfig config = this.getServletConfig();
String paramValue = config.getInitParamter("paramName");

Also note that since GenericServletclass implements ServletConfiginterface, your servlet class is also ServletConfig (implies this = this.getServletConfig()). Hence you can also get DD elements <init-param>directly by:

另请注意,由于GenericServlet类实现了ServletConfig接口,因此您的 servlet 类也是 ServletConfig(暗示this = this.getServletConfig())。因此,您还可以<init-param>通过以下方式直接获取 DD 元素:

String paramValue = this.getInitParamter("paramName");

.

.

You can try this by having same param-name in both DD elements with different values and then print it in your servlet.

您可以通过在具有不同值的两个 DD 元素中使用相同的 param-name 来尝试此操作,然后将其打印在您的 servlet 中。