`
hongwei3344661
  • 浏览: 29842 次
  • 性别: Icon_minigender_1
文章分类
社区版块
存档分类
最新评论

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

阅读更多
<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.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.
 
如:
[html] view plain copy
 
  1. <!-- 加载spring的配置文件 -->  
  2. <context-param>  
  3.     <param-name>contextConfigLocation</param-name>  
  4.     <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-INF/jason-servlet.xml</param-value>  
  5. </context-param>  
  6. <listener>  
  7.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  8. </listener>  

[java] view plain copy
 
  1. public class SysListener extends HttpServlet implements ServletContextListener {private static final Log logger = LogFactory.getLog(SysListener.class);public void contextDestroyed(ServletContextEvent sce) {   //用于在容器关闭时,操作  
  2. }//用于在容器开启时,操作public void contextInitialized(ServletContextEvent sce) {  
  3.    String rootpath = sce.getServletContext().getRealPath("/");  
  4.    System.out.println("-------------rootPath:"+rootpath);   if (rootpath != null) {  
  5.     rootpath = rootpath.replaceAll("\\\\", "/");  
  6.    } else {  
  7.     rootpath = "/";  
  8.    }  
  9.    if (!rootpath.endsWith("/")) {  
  10.     rootpath = rootpath + "/";  
  11.    }  
  12.    Constant.ROOTPATH = rootpath;  
  13.    logger.info("Application Run Path:" + rootpath);  
  14.    String urlrewrtie = sce.getServletContext().getInitParameter("urlrewrite");  
  15.    boolean burlrewrtie = false;  
  16.    if (urlrewrtie != null) {  
  17.     burlrewrtie = Boolean.parseBoolean(urlrewrtie);  
  18.    }  
  19.    Constant.USE_URL_REWRITE = burlrewrtie;  
  20.    logger.info("Use Urlrewrite:" + burlrewrtie);  
  21.    其它略之....    }}  
  22.    /*最终输出 
  23.    -------------rootPath:D:\tomcat_bbs\webapps\BBSCS_8_0_3\ 
  24.    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/ 
  25.    2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Use Urlrewrite:true 
  26.    2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Use Cluster:false 
  27.    2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]SERVLET MAPPING:*.bbscs 
  28.    2009-06-09 21:51:46,573 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Post Storage Mode:1 
  29.    */  

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

(2)servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下:
[html] view plain copy
 
  1. <servlet>  
  2.     <servlet-name>MainServlet</servlet-name>  
  3.     <servlet-class>com.wes.controller.MainServlet</servlet-class>  
  4.     <init-param>  
  5.        <param-name>param1</param-name>  
  6.        <param-value>avalible in servlet init()</param-value>  
  7.     </init-param>  
  8.     <load-on-startup>0</load-on-startup>  
  9. </servlet>  

在servlet中可以通过代码分别取用:
[java] view plain copy
 
  1. package com.wes.controller;import javax.servlet.ServletException;  
  2. import javax.servlet.http.HttpServlet;public class MainServlet extends HttpServlet ...{    public MainServlet() ...{  
  3.         super();  
  4.      }  
  5.     public void init() throws ServletException ...{  
  6.          System.out.println("下面的两个参数param1是在servlet中存放的");  
  7.          System.out.println(this.getInitParameter("param1"));  
  8.          System.out.println("下面的参数是存放在servletcontext中的");  
  9.         System.out.println(getServletContext().getInitParameter("context/param"));  
  10.       }  
  11. }  

第一种参数在servlet里面可以通过getServletContext().getInitParameter("context/param")得到
第二种参数只能在servlet的init()方法中通过this.getInitParameter("param1")取得.
分享到:
评论

相关推荐

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

    &lt;context-param&gt; 和&lt;init-param&gt;的 区别代码 博文链接:https://xhy0422.iteye.com/blog/46319

    修改后的 fckedit.jar

    class&gt;&lt;br&gt; &lt;init-param&gt;&lt;br&gt; &lt;param-name&gt;baseDir&lt;/param-name&gt;&lt;br&gt; &lt;param-value&gt;/ userDir&lt;/param-value&gt;&lt;br&gt; &lt;/init-param&gt;&lt;br&gt; &lt;init-param&gt;&lt;br&gt; &lt;param-name&gt;debug&lt;/param-name&gt;&lt;br&gt; &lt;param-value...

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

    本篇文章是对web.xml中在Servlet中获取context-param和init-param内的参数进行了详细的分析介绍,需要的朋友参考下

    基于MyEclipse搭建maven+springmvc整合图文教程(含源码0

    &lt;/context-param&gt; &lt;!-- Spring的log4j监听器 --&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;!-- 字符集 过滤器 --&gt; &lt;filter&gt; ...

    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&gt; &lt;listener-...

    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;!-- 用于单点退出,该...

    生活轨迹SSH服务端

    &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;!-- &lt;param-value&gt;/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml&lt;/param-value&gt; --&gt; &lt;param-value&gt;classpath:...

    DOS命令使用方法(超全).

    DOS命令使用方法(超全). ... http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt; &lt;!-- 下边这是加入spring配置 --&gt;... &lt;init-param&gt; &lt;param-name&gt;encoding&lt;/param-name&gt; &lt;param-value&gt;UTF-8&lt;/param-value&gt;

    springmvcwebjspWeb-Return.zip

    &lt;init-param&gt; &lt;!-- 例外的页面路径,因计算currentUrlDecode耗时,例外的路径可以不用处理--&gt; &lt;param-name&gt;excludedPages&lt;/param-name&gt; &lt;param-value&gt;resources/,persons/login/,persons/register/...

    Spring MVC 入门实例

    8 &lt;context-param&gt; 9 &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; 10 &lt;param-value&gt; 11 /WEB-INF/database.xml 12 /WEB-INF/applicationContext.xml 13 &lt;/param-value&gt; 14 &lt;/context-param&gt; 15 16 &lt;listener...

    JSF文件上传

    &lt;context-param&gt; &lt;param-name&gt;javax.faces.STATE_SAVING_METHOD&lt;/param-name&gt; &lt;param-value&gt;client&lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- Context Listener creates and sets the application handler --&gt...

    TOMCAT快速上手

    Tomcat 中配置一个Web应用 1 conf/server.xml 在&lt;host&gt;&lt;/host&gt;标记中间增加: ... &lt;param-name&gt;listings&lt;/param-name&gt; &lt;param-value&gt;false&lt;/param-value&gt; &lt;/init-param&gt; 将false改成true即可,开发调试阶段很有用。

    基于EXT SSI的简单树实现

    &gt;&lt;nodes&gt;&lt;node id="40" text="咨询" /&gt;&lt;node id="20" text="建议" /&gt;&lt;node id="10" text="投诉" /&gt;&lt;node id="50" text="预约" /&gt;&lt;node id="30" text="报障" /&gt;&lt;node id="1090464" text="集团自查工单修改" /&gt;&lt;node ...

    轻量级java web MVC框架

    &lt;context-param&gt; &lt;param-name&gt;ScanPackage&lt;/param-name&gt; &lt;param-value&gt;com.mvc.controller&lt;/param-value&gt; &lt;/context-param&gt; 容器在启动时候,会将com.mvc.controller下所有映射路径绑定处理方法上,假如在扫描包...

    SpringMVC-Mybatis-Shiro-redis-master 权限集成缓存中实例

    &lt;/context-param&gt; 4.项目Maven打包问题。 打包的时候,不同版本的 Eclipse 还有IDEA 会有打包打不进去Mapper.xml 文件,这个时候要加如下代码(群里同学提供的)。 &lt;resources&gt; &lt;resource&gt; &lt;directory&gt;src/...

    Spring.html

    --全局初始化参数--&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt; &lt;/context-param&gt; 4.在Servlet中使用...

Global site tag (gtag.js) - Google Analytics