init-param和context-param [英] init-param and context-param

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

问题描述

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

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

推荐答案

< init-param> < 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"));

如果您想存储整个应用程序常用的数据,如果它不会经常更改您可以使用< context-param> 而不是 servletContext.setAttribute()方法应用程序上下文。有关使用< context-param> VS ServletContext.setAttribute()的更多信息,请查看此< a href =https://stackoverflow.com/a/11046800/695657>问题。 context-param 在标签 web-app 下声明。
您可以声明并访问< context-param> ,如下所示

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");

这篇关于init-param和context-param的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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