`

web.xml文件之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.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.
 
如:
<!-- 加载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 SysListenerextends HttpServlet implements ServletContextListener{
private static final Log logger = LogFactory.getLog(SysListener.class);
public voidcontextDestroyed(ServletContextEvent sce){
  //用于在容器关闭时,操作
}
 
//用于在容器开启时,操作
public voidcontextInitialized(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")取得.
分享到:
评论

相关推荐

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

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

    Spring MVC 框架应用实例

    org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt; &lt;filter-class&gt; org.springframework.web.filter.CharacterEncodingFilter...

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

    &lt;param-value&gt;classpath*:applicationContext-*.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- 这是spring 监听 --&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-...

    生活轨迹SSH服务端

    -- &lt;param-value&gt;/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml&lt;/param-value&gt; --&gt; &lt;param-value&gt;classpath:beans.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;filter-name&gt;struts2...

    Struts2+Spring3+MyBatis3完整实例

    网上的东西好大多都不能直接用,自己结合网上资料做了一个Struts2+Spring3+MyBatis3的测试工程,JUnit测试用例和WEB服务。 内涵完整jar包,解压直接可用,包括一个表文件。 Eclipse3.2+Tomcat/5.5+jdk1.5.0_17 - ...

    修改后的 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; ...

    CAS客户端JAR包版本3.3.3

    配置web.xml &lt;!--退出--&gt; &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-...

    Spring MVC 入门实例

    context-param 标签指明我们的配置文件还有 /WEB-INF/database.xml 和 /WEB-INF/applicationContext.xml. ContextLoaderListener(listener 标签) 由此得知配置文件是哪些, 它会将它们载入. 因为我们将 ...

    基于EXT SSI的简单树实现

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;context-param&gt; &lt;description&gt;spring初始配置 &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-...

    springmvcwebjspWeb-Return.zip

    &lt;param-value&gt;resources/,persons/login/,persons/register/&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;!-- 如果session属性sessionString值,非空则支持回退;此处支持扩展支持仅仅登录后才能回退--&...

    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即可,开发调试阶段很有用。

    JSF文件上传

    Web-xml文件如下: &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"&gt; &lt;web-app...

    轻量级java web MVC框架

    &lt;param-value&gt;/WEB-INF/view/&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;suffix&lt;/param-name&gt; &lt;param-value&gt;.jsp&lt;/param-value&gt; &lt;/init-param&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;...

    Spring-Framework-5:Spring框架5,Hibernate,Jsp,Spring Mvc,

    Spring_Framework_5_denemeleri 1-弹簧框架依赖项注入... 然后在bean.xml中使用init-methods和param方法 4-弹簧注释(b) 这个项目是关于春天的注解(b)。 @Autowired和@Qualifier(“ ...”)用法。 -第一步:在b

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

    &lt;param-value&gt;/WEB-INF/dispatcher-servlet.xml&lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt; &lt;url-pattern&gt;*.do...

    ssh(structs,spring,hibernate)框架中的上传下载

    WEB-INF下的applicationContext.xml为Spring的配置文件,struts-config.xml为Struts的配置文件,file-upload.jsp为文件上传页面,file-list.jsp为文件列表页面。  本文后面的章节将从数据持久层->业务层->Web层的...

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

    &lt;param-name&gt;spring.liveBeansView.mbeanDomain&lt;/param-name&gt; &lt;param-value&gt;dev&lt;/param-value&gt; &lt;/context-param&gt; 4.项目Maven打包问题。 打包的时候,不同版本的 Eclipse 还有IDEA 会有打包打不进去Mapper.xml ...

    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中使用...

    cms后台管理

    &lt;param-value&gt;/WEB-INF/config/jeecms-servlet-front.xml&lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;2&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;JeeCmsFront&lt;/servlet-name&gt; &lt;url-...

    将 Flex 集成到 Java EE 应用程序的最佳实践(完整源代码)

    &lt;param-value&gt;/WEB-INF/flex/services-config.xml&lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;0&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;messageBroker&lt;/servlet-name&gt; &lt;url...

Global site tag (gtag.js) - Google Analytics