ServletConfig & ServletContext 区别

本文详细介绍了web.xml中的关键配置项,如<context-param>、<listener>和<servlet>等,解释了它们的作用及如何在Servlet容器中进行初始化。

<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应用被关闭时才被销毁。

  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中获取。

<!-- 配置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的键");

092745_i7LC_3434392.png

ServletConfig

 在Servlet的配置文件中可多个<init-param>为servlet配置一些初始化参数。web容器创建servlet实例对象时自动将初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。

092617_RuzZ_3434392.png


ServletConfig对象中维护了ServletContext对象的引用: ServletConfig.getServletContext()

134801_ViUs_3434392.png

 

 

转载于:https://my.oschina.net/u/3434392/blog/1498513

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值