servlet init parameters和context init parameters的區別

Context init parameters   在web.xml中以下

<web-app ...>
<context-param>
<param-name>foo</param-name>
<param-value>bar</param-value>
</context-param>
<!-- other stuff including
servlet declarations -->
</web-app>
Within the <web-app> element but NOT

within a specific <servlet> element web



Servlet init parameters在web.xml中以下 tomcat

<servlet> app

<servlet-name> webapp

BeerParamTests
</servlet-name>
<servlet-class>
TestInitParams
</servlet-class>
<init-param>
<param-name>foo</param-name>
<param-value>bar</param-value>
</init-param>
<!-- other stuff -->
</servlet>
Within the <servlet> element for each

specific servlet jsp


經過這樣的代碼獲取ServletContext ide

getServletContext().getInitParameter(「foo」); this

經過這樣的代碼獲取ServletConfig xml

getServletConfig().getInitParameter(「foo」); ci

Availability


To any servlets and JSPs that are part element

of this web app.(ServletContext是對於任何的jsp或者servlet都是有效的,能夠訪問的)



To only the servlet for which the <init-param> was
configured.
(Although the servlet can choose to make it more

widely available by storing it in an attribute.)

(ServletConfig對於特定配置好<inti-param>的servlet有效)

對於每個servlet都有一個servletConfig

對於整個webapp有一個ServletContext

當web app初始化的時候,容器(好比tomcat)讀取web.xml,而且爲<context-param>建立一個鍵值對,

容器建立一個新的ServletContext實例,

容器將鍵值對傳遞給建立好的實例ServletContext

web app中的任何一部分都有訪問這個ServletContext的權限

相關文章
相關標籤/搜索