`
天然呆的爱死你了呢
  • 浏览: 11922 次
社区版块
存档分类
最新评论

context-param和init-param区别(转载)

阅读更多
<context-param>与<init-param>的区别与作用


spring   2009-11-04 16:49    阅读39    评论0 

字号: 大  中  小




<context-param>的作用:
web.xml的配置中<context-param>配置作用
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.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.


如:
<!-- 加载spring的配置文件 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-

INF/jason-servlet.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


又如: --->自定义context-param,且自定义listener来获取这些信息

<context-param>
    <param-name>urlrewrite</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>cluster</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>servletmapping</param-name>
    <param-value>*.bbscs</param-value>
</context-param>
<context-param>
    <param-name>poststoragemode</param-name>
    <param-value>1</param-value>
</context-param>
<listener>
    <listener-class>com.laoer.bbscs.web.servlet.SysListener</listener-class>
</listener>


public class SysListener extends HttpServlet implements ServletContextListener {

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

public void contextDestroyed(ServletContextEvent sce) {

   //用于在容器关闭时,操作
}


//用于在容器开启时,操作

public void contextInitialized(ServletContextEvent sce) {
   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);
   其它略之....

   }

}
   /*最终输出
   -------------rootPath:D:\tomcat_bbs\webapps\BBSCS_8_0_3\
   2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]

Application Run Path:D:/tomcat_bbs/webapps/BBSCS_8_0_3/
   2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]

Use Urlrewrite:true
   2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]

Use Cluster:false
   2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]

SERVLET MAPPING:*.bbscs
   2009-06-09 21:51:46,573 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]

Post Storage Mode:1
   */


context-param和init-param区别
web.xml里面可以定义两种参数:
(1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下:
<context-param>
           <param-name>context/param</param-name>
           <param-value>avalible during application</param-value>
</context-param>

(2)servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下:
<servlet>
    <servlet-name>MainServlet</servlet-name>
    <servlet-class>com.wes.controller.MainServlet</servlet-class>
    <init-param>
       <param-name>param1</param-name>
       <param-value>avalible in servlet init()</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
</servlet>

在servlet中可以通过代码分别取用:
package com.wes.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class MainServlet 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")取得.
分享到:
评论

相关推荐

    spring配置中<context-param> 和<init-param>的 区别

    `&lt;context-param&gt;` 和 `&lt;init-param&gt;` 的主要区别在于作用范围和目标对象。`&lt;context-param&gt;` 作用于整个Web应用,影响Spring的ApplicationContext;而 `&lt;init-param&gt;` 则是针对特定Servlet或Filter的个性化配置。...

    <context-param>与<init-param>的区别与作用

    总结一下,`&lt;context-param&gt;`和`&lt;init-param&gt;`的主要区别在于: 1. **作用范围**:`&lt;context-param&gt;`是全局的,应用于整个Web应用程序;而`&lt;init-param&gt;`是局部的,仅应用于指定的Servlet或Filter。 2. **加载时机*...

    解析web.xml中在Servlet中获取context-param和init-param内的参数

    其中,`context-param`和`init-param`是两个重要的元素,用于设置应用级和Servlet级的初始化参数。理解它们的用法和如何在Servlet中获取这些参数对于构建和维护Web应用程序至关重要。 首先,`context-param`是用来...

    修改后的 fckedit.jar

    web.xml 中配置 完善了对上传图片的验证 &lt;context-param&gt; &lt;param-name&gt;FCKAllowedExtensionsImage&lt;/param-name&gt; &lt;param-value&gt;jpg|gif|jpeg|png|bmp &lt;/param-value&gt; &lt;/context-param&gt; ...

    RED5安装与配置 RED5

    &lt;param-value&gt;default.context&lt;/param-value&gt; &lt;/context-param&gt; ``` 5. **log4jConfigLocation**:指定日志配置文件的位置。 ```xml &lt;context-param&gt; &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt; &lt;param...

    RED5安装与配置

    &lt;param-value&gt;default.context&lt;/param-value&gt; &lt;/context-param&gt; ``` 5. **log4jConfigLocation**:日志配置文件的位置。用于指定日志配置文件的位置。 ```xml &lt;context-param&gt; &lt;param-name&gt;log4...

    WebLogic配置代理转发.pdf

    这些参数都可以在`&lt;init-param&gt;`标签中设置,以达到细微调整代理转发行为的目的。例如: ```xml &lt;init-param&gt; &lt;param-name&gt;PathTrim&lt;/param-name&gt; &lt;param-value&gt;/proxy&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt;...

    jsf2.0新特性及richface优化总结

    - **加载策略**:`&lt;context-param&gt;org.richfaces.LoadStyleStrategy&lt;/context-param&gt;`和`&lt;context-param&gt;org.richfaces.LoadScriptStrategy&lt;/context-param&gt;`均设为`all`,确保在首次请求时加载所有相关样式和脚本...

    web.xml配置[归纳].pdf

    当我们需要改变`applicationContext.xml`的路径时,可以在`web.xml`中使用`&lt;context-param&gt;`元素来指定。例如: ```xml &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;...

    web.xml中的listen

    - `&lt;context-param&gt;`标签中的`contextConfigLocation`参数指定了Spring配置文件的位置,可以是多个文件,之间用逗号分隔。 #### 二、监听自定义Java类 除了加载Spring配置文件之外,`web.xml`还可以用于监听...

    java学习-web.xml配置详解实用.pdf

    该元素包含两个子元素:`&lt;param-name&gt;`和`&lt;param-value&gt;`。其中,`&lt;param-name&gt;`元素用于定义参数名称,该名称在整个Web应用程序中必须是惟一的。`&lt;param-value&gt;`元素用于定义参数值。 例如: ```xml &lt;context-...

    Servlet编程范例(4)

    在特定的生命周期阶段,Servlet容器可能会触发监听器(Listener)中的特定事件处理方法,例如`contextInitialized(ServletContextEvent sce)`和`contextDestroyed(ServletContextEvent sce)`,这些监听器可以帮助...

    CAS客户端JAR包版本3.3.3

    &lt;context-param&gt; &lt;param-name&gt;casServerLogoutUrl&lt;/param-name&gt; &lt;param-value&gt;http://192.168.156.120:8080/cas/logout&lt;/param-value&gt;&lt;!--server cas 地址--&gt; &lt;/context-param&gt; &lt;!-- 用于单点退出,该...

    log4j.properties文件放置在其它目录下

    &lt;context-param&gt; &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/log4j.properties&lt;/param-value&gt; &lt;/context-param&gt; 然后,我们需要写个 servlet,以便在 Servlet 中指定 log4j 配置文件的...

    润乾报表在web工程中的部署

    &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;dataSource&lt;/param-name&gt; &lt;param-value&gt;test01,oracle,gb2312&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;cachedParamsTimeout&lt;/param-name&gt; &lt;param-...

    Spring MVC 框架应用实例

    &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/database.xml /WEB-INF/applicationContext.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;listener-class&gt; ...

Global site tag (gtag.js) - Google Analytics