context-param與init-param的區別與作用

web.xml的配置中配置作用

1. 啓動一個WEB項目的時候,容器(如:Tomcat)會去讀它的配置文件web.xml.讀兩個節點:

2.緊接着,容器創建一個ServletContext(上下文),這個WEB項目所有部分都將共享這個上下文.

3.容器將

轉化爲鍵值對,並交給ServletContext.

4.容器創建

中的類實例,即創建監聽.

5.在監聽中會有contextInitialized(ServletContextEvent args)初始化方法,在這個方法中獲得ServletContext =ServletContextEvent.getServletContext();

context-param的值 =ServletContext.getInitParameter("context-param的鍵");

6.得到這個context-param的值之後,你就可以做一些操作了.注意,這個時候你的WEB項目還沒有完全啓動完成.這個動作會比所有的Servlet都要早.

換句話說,這個時候,你對中的鍵值做的操作,將在你的WEB項目完全啓動之前被執行.

7.舉例.你可能想在項目啓動之前就打開數據庫.

那麼這裏就可以在中設置數據庫的連接方式,在監聽類中初始化數據庫的連接.

8.這個監聽是自己寫的一個類,除了初始化方法,它還有銷燬方法.用於關閉應用前釋放資源.比如說數據庫連接的關閉.

如:

contextConfigLocation

/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-

INF/jason-servlet.xml

org.springframework.web.context.ContextLoaderListener

又如: --->自定義context-param,且自定義listener來獲取這些信息

urlrewrite

false

cluster

false

servletmapping

*.bbscs

poststoragemode

1

com.laoer.bbscs.web.servlet.SysListener

public classSysListener extendsHttpServlet implements ServletContextListener {

private staticfinal Log logger = LogFactory.getLog(SysListener.class);

public void contextDestroyed(ServletContextEventsce) {

//用於在容器關閉時,操作

}

//用於在容器開啓時,操作

public void contextInitialized(ServletContextEventsce) {

String rootpath = sce.getServletContext().getRealPath("/");

System.out.println("-------------rootPath:"+rootpath);

if(rootpath != null) {

rootpath = rootpath.replaceAll("\\\\","/");

} else {

rootpath = "/";

}

if (!rootpath.endsWith("/")) {

rootpath = rootpath + "/";

}

Constant.ROOTPATH = rootpath;

logger.info("Application Run Path:" + rootpath);

String urlrewrtie = sce.getServletContext().getInitParameter("urlrewrite");

boolean burlrewrtie = false;

if (urlrewrtie != null) {

burlrewrtie = Boolean.parseBoolean(urlrewrtie);

}

Constant.USE_URL_REWRITE = burlrewrtie;

logger.info("Use Urlrewrite:" + burlrewrtie);

其它略之....

}

}

context-param和init-param區別

web.xml裏面可以定義兩種參數:

(1)application範圍內的參數,存放在servletcontext中,在web.xml中配置如下:

context/param

avalible during application

(2)servlet範圍內的參數,只能在servlet的init()方法中取得,在web.xml中配置如下:

MainServlet

com.wes.controller.MainServlet

param1

avalible in servletinit()

0

在servlet中可以通過代碼分別取用:

package com.wes.controller;

importjavax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

public classMainServlet extends HttpServlet ...{

public MainServlet() ...{

super();

}

public void init() throws ServletException ...{

System.out.println("下面的兩個參數param1是在servlet中存放的");

System.out.println(this.getInitParameter("param1"));

System.out.println("下面的參數是存放在servletcontext中的");

System.out.println(getServletContext().getInitParameter("context/param"));

}

}

第一種參數在servlet裏面可以通過getServletContext().getInitParameter("context/param")得到

第二種參數只能在servlet的init()方法中通過this.getInitParameter("param1")取得.

定義

context-param元素含有一對參數名和參數值,用作應用的ServletContext上下文初始化參數。參數名在整個Web應用中必須是惟一的。

編輯本段描述參數

param-name 子元素包含有參數名,而param-value子元素包含的是參數值。作爲選擇,可用description子元素來描述參數。

下面是一個含有context-param元素的有效部署描述符:

PUBLIC "-//Sun Microsystems, Inc.//DTD WebApplication 2.3//EN"

jdbcDriver

com.mysql.jdbc.Driver

The load-on-startup element indicates that this servletshould be loaded (instantiated and have its init() called) on the startup ofthe web application. The optional contents of these element must be an integerindicating the order in which the servlet should be loaded. If the value is anegative integer, or the element is not present, the container is free to loadthe servlet whenever it chooses. If the value is a positive integer or 0,the container must load and initialize the servlet as the application isdeployed. The container must guarantee that servlets marked with lower integersare loaded before servlets marked with higher integers. The container maychoose the order of loading of servlets with the same load-on-start-up value.

這個 load-on-startup 元素 在 web 應用啓動的時候指定了servlet被加載的順序,它的值必須是一個整數。如果它的值是一個負整數或是這個元素不存在,那麼容器會在該servlet被調用的時候,加載這個servlet 。如果值是正整數或零,容器在配置的時候就加載並初始化這個servlet,容器必須保證值小的先被加載。如果值相等,容器可以自動選擇先加載誰。

發佈了26 篇原創文章 · 獲贊 7 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章