`
- 浏览:
36763 次
- 性别:
- 来自:
南京
-
1.启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context-param></context-param>
2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.
3.容器将<context-param></context-param>转化为键值对,并交给ServletContext.
4.容器创建<listener></listener>中的类实例,即创建监听.
5.在监听中会有contextInitialized(ServletContextEvent args)初始化方法,在这个方法中获得
ServletContext = ServletContextEvent.getServletContext();
context-param的值 = ServletContext.getInitParameter("context-param的键");
6.得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早.
换句话说,这个时候,你对<context-param>中的键值做的操作,将在你的WEB项目完全启动之前被执行.
7.举例.你可能想在项目启动之前就打开数据库.
那么这里就可以在<context-param>中设置数据库的连接方式,在监听类中初始化数据库的连接.
8.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
web.xml中<context-param>等配置的作用 了解 web.xml 的配置信息和加载顺序对于理解框架的流程至关重要。在 web.xml 中,<context-param> 配置起着非常重要的作用,它在 web 项目启动时发挥着关键的作用。 首先,...
在Java Web开发中,`<context-param>`和`<init-param>`是两种常见的配置元素,它们都用于传递参数,但作用范围和应用场景有所不同。本文将详细介绍两者之间的区别以及它们在实际开发中的应用。 首先,`<context-...
在Spring框架中,`<context-param>` 和 `<init-param>` 是两种不同的参数配置方式,它们在Web应用的初始化阶段起着关键作用。了解这两者的区别是优化和理解Spring应用程序运行时行为的重要知识点。 首先,`<context...
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> ``` 这个配置指定了 Log4j 配置文件的位置为 /WEB-INF/classes...
在web.xml文件中,添加了resource-ref配置:<resource-ref> <description>SQL Server Datasource</description> <res-ref-name>jdbc/DBUtil</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>...
-- context-param 元素用来设定web站台的环境参数(context),它包含两个子元素:param-name和param-value.,如spring的典型配置 --> <context-param> <!-- 设定Context名称 --> <param-name>contextConfigLocation...
<param-name>context/param</param-name> <param-value>avalible during application</param-value> </context-param> ``` 在这里,`<param-name>`标签定义了参数名,`<param-value>`标签定义了参数值。要从...
Spring的初始化配置通常通过`<context-param>`和`<listener>`标签来完成。`context-param`用于指定Spring上下文配置文件的位置,通常是一个或多个路径,用逗号分隔,如`classpath*:spring/spring-config.xml`。`...
<property name="defaultContext" ref="defaultContext" /> </bean> <bean id="defaultServer" class="org.restlet.ext.jetty.Server"> <constructor-arg> <value>org.restlet.ext.jetty.JettyServer</value> ...
<context-param> <param-name>extremecomponentsResourceBundleLocation</param-name> <param-value>com.itorgan.tags.extreme.extremetableResourceBundle</param-value> </context-param> ``` 这将使得 Ectable...
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/applicationContext*.xml</param-value> </context-param> <filter> <filter-name>springSecurityFilterChain</...
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> ...
`<context-param>`元素用于定义Web应用程序的环境参数(Context)。该元素包含两个子元素:`<param-name>`和`<param-value>`。其中,`<param-name>`元素用于定义参数名称,该名称在整个Web应用程序中必须是惟一的。`...
<param-value>true</param-value> </init-param> <init-param> <param-name>redirectAfterValidation</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>encoding</...
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web....
<param-value>default.context</param-value> </context-param> ``` 5. **log4jConfigLocation**:指定日志配置文件的位置。 ```xml <context-param> <param-name>log4jConfigLocation</param-name> <param...