初始化参数和上下文参数 [英] init-param and context-param

查看:25
本文介绍了初始化参数和上下文参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别!?

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

推荐答案

是静态参数,它们被存储在 web.xml 文件中.如果您有任何不经常更改的数据,您可以将其存储在其中之一中.

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

如果您想存储限于特定 servlet 范围的特定数据,那么您可以使用 <init-param> .您在 <init-param> 只能被那个特定的 servlet 访问.init-param<servlet> 标签内声明.>

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-param is declared inside the <servlet> tag.

<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 中的这些参数:

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

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

如果您想存储整个应用程序通用的数据,并且如果它不经常更改,则可以使用 而不是 >servletContext.setAttribute() 应用程序上下文的方法.有关 VS ServletContext.setAttribute() 用法的更多信息,请查看此 问题.context-param 在标签 web-app 下声明.您可以按如下方式声明和访问

If you want to store data which is common for whole application and 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-param are declared under the tag web-app. You can declare and access the <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>

在 JSP 或 Servlet 中的应用程序中的使用

Usage in the application either in a JSP or Servlet

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

这篇关于初始化参数和上下文参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆