ServletConfig & ServletContext 區別

context-param

web.xml的配置項,標識application範圍內的參數,加載比其餘任何Servlet都早。前端

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring 刷新Introspector防止內存泄露 -->
<listener>
	<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- c1-start 配置spring的父上下文 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath*:spring/spring.xml,classpath*:spring/applicationContext-per.xml
    </param-value>
</context-param>

<!-- 登陸失敗Url -->
<context-param>
	<param-name>failUrl</param-name>
	<param-value>/</param-value>
</context-param>
  1. 啓動web容器將讀取web.xml上的節點:<listener>、<context-param>
  2. 容器將<context-param>轉化成值對保存在web容器建立的application內共享的ServletContext中;
  3. ServletContextListener(ContextLoaderListener ) 監聽器中contextInitialized( ServletContextEvent sce ) 初始化方法中得到:
    servletContext = ServletContextEvent.getServletContext()
    /** 
    	 * Implementations of this interface receive notifications about
    	 * changes to the servlet context of the web application they are
    	 * part of.
    	 * To receive notification events, the implementation class
    	 * must be configured in the deployment descriptor for the web
    	 * application.
    	 * @see ServletContextEvent
    	 * @since	v 2.3
    	 */
    
    public interface ServletContextListener extends EventListener {
    	/**
    	 ** Notification that the web application initialization
    	 ** process is starting.
    	 ** All ServletContextListeners are notified of context
    	 ** initialization before any filter or servlet in the web
    	 ** application is initialized.
    	 */
    
        public void contextInitialized ( ServletContextEvent sce );
    
    	/**
    	 ** Notification that the servlet context is about to be shut down.
    	 ** All servlets and filters have been destroy()ed before any
    	 ** ServletContextListeners are notified of context
    	 ** destruction.
    	 */
        public void contextDestroyed ( ServletContextEvent sce );
    }

ServletContext

WEB容器啓動時會建立一個表明當前web應用的上下文ServletContext對象,一般稱爲context 域對象。
不一樣的Web應用,ServletContext各自獨立存在。只在Web應用被關閉時才被銷燬。java

  1. 獲取到整個web應用的配置信息<context-param>
     context-param的值 = ServletContext.getInitParameter("context-param的鍵");
  2. 實現Servlet間通信。   主要是4個有關 Attribute 的方法:setAttribute()、getAttribute()、removeAttribute()、getAttributeNames()
    ServletContext context =this.getServletContext(); // servletContext域對象 
    context.setAttribute("data","共享數據"); // 向域中存了一個data屬性
    // 在另外一個servlet中,能夠使用以下語句來獲取域中的data屬性
    ServletContext context =this.getServletContext();
    String value = (String)context.getAttribute("data");  // 獲取域中的data屬性
  3. 讀取資源文件
    String path= "/WEB-INF/classes/db.properties"; //默認'/webapp'下目錄
    //返回資源文件的讀取字節流
    InputStream in =this.getServletContext().getResourceAsStream(path);
    //得到文件的完整絕對路徑path,再使用字節流讀取path下的文件
    String allPath =this.getServletContext().getRealPath(path);
    //得到一個url對象,調用該類的openStream方法返回一個字節流,讀取數據
    URL url=this.getServletContext().getResource(path);

init-param

 servlet範圍內的參數,只在指定的servlet中獲取。web

<!-- 配置springmvc的前端控制器,同時初始化子上下文 -->
<servlet>
	<servlet-name>springmvc</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:spring/spring-mvc.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>springmvc</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

init-param的值 = Servlet.getInitParameter("init-param的鍵");spring

ServletConfig

 在Servlet的配置文件中可多個<init-param>爲servlet配置一些初始化參數。web容器建立servlet實例對象時自動將初始化參數封裝到ServletConfig對象中,並在調用servlet的init方法時,將ServletConfig對象傳遞給servlet。spring-mvc


ServletConfig對象中維護了ServletContext對象的引用: ServletConfig.getServletContext()。mvc

相關文章
相關標籤/搜索