web.xml配置详解系列一

http://blog.csdn.net/liaoxiaohua1981/article/details/6771043

  •  格式定义:
  1. <context-param>  
  2. <param-name>contextConfigLocation</param-name>  
  3. <param-value>contextConfigLocationValue></param-value>  
  4. </context-param>  

作用:该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数。

param-name 设定上下文的参数名称。必须是唯一名称

param-value 设定的参数名称的值

  • 初始化过程:
    1. 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>。
    2. 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。
    3. 接着容器会将读取到<context-param>转化为键值对,并交给ServletContext。
    4. 容器创建<listener></listener>中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)。
    5. 在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这个方法中可以通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比如说数据库连接的关闭。
    6. 得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早。

由上面的初始化过程可知容器对于web.xml的加载过程是context-param >> listener  >> fileter  >> servlet

  •  如何使用
  1. 页面中

    ${initParam.contextConfigLocation}

  2. Servlet中
    String paramValue=getServletContext().getInitParameter("contextConfigLocation")

 

    声明:此内容为整理所得

  • 定义
  1. <listener>  
  2. <listen-class>com.myapp.MyListener</listen-class>  
  3. </listener>  
  • 作用
    该元素用来注册一个监听器类。可以收到事件什么时候发生以及用什么作为响应的通知。事件监听程序在建立、修改和删除会话或servlet环境时得到通知。常与context-param联合使用。
  • listen-class 指定监听类,该类继承ServletContextListener 包含初始化方法contextInitialized(ServletContextEvent event) 和 销毁方法contextDestoryed(ServletContextEvent event)
  • 示例:初始化日志配置文件
  1. <!--初始化日志配置文件 -->    
  2. <listener>    
  3.     <listener-class>    
  4.         com.myapp.LogbackConfigListener    
  5.     </listener-class>    
  6. </listener>    
  7. <context-param>    
  8.     <param-name>logbackConfigLocation</param-name>    
  9.     <param-value>WEB-INF/logback.xml</param-value>    
  10. </context-param>    
  1. /** 
  2.  *  
  3.  */  
  4. package com.myapp;  
  5.   
  6. import javax.servlet.ServletContextEvent;  
  7. import javax.servlet.ServletContextListener;  
  8. import org.slf4j.Logger;    
  9. import org.slf4j.LoggerFactory;    
  10. import ch.qos.logback.classic.LoggerContext;    
  11. import ch.qos.logback.classic.joran.JoranConfigurator;    
  12. import ch.qos.logback.core.joran.spi.JoranException;   
  13. /** 
  14.  * @author louisliao 
  15.  * 
  16.  */  
  17. public class LogbackConfigListener implements ServletContextListener {  
  18.   
  19.     private static final Logger logger = LoggerFactory.getLogger(LogbackConfigListener.class);    
  20.     private static final String CONFIG_LOCATION = "logbackConfigLocation";    
  21.     /* (non-Javadoc) 
  22.      * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent) 
  23.      */  
  24.     public void contextDestroyed(ServletContextEvent arg0) {  
  25.         // TODO Auto-generated method stub  
  26.   
  27.     }  
  28.   
  29.     /* (non-Javadoc) 
  30.      * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent) 
  31.      */  
  32.     public void contextInitialized(ServletContextEvent event) {  
  33.         // TODO Auto-generated method stub  
  34.         String logbackConfigLocation = event.getServletContext().getInitParameter(CONFIG_LOCATION);    
  35.         String fn = event.getServletContext().getRealPath(logbackConfigLocation);    
  36.         try {    
  37.             LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();    
  38.             loggerContext.reset();    
  39.             JoranConfigurator joranConfigurator = new JoranConfigurator();    
  40.             joranConfigurator.setContext(loggerContext);    
  41.             joranConfigurator.doConfigure(fn);    
  42.             logger.debug("loaded slf4j configure file from {}", fn);    
  43.         }    
  44.         catch (JoranException e) {    
  45.             logger.error("can loading slf4j configure file from " + fn, e);    
  46.         }    
  47.   
  48.     }  
  49.   
  50. }  
  • 定义
  1. <pre class="html" name="code"><filter>  
  2. <filter-name>encodingfilter</filter-name>  
  3. <filter-class>com.my.app.EncodingFilter</filter-class>  
  4. <init-param>  
  5. <param-name>encoding</param-name>  
  6. <param-value>UTF-8</param-value>  
  7. </init-param>  
  8. </filter>  
  9. <filter-mapping>  
  10. <filter-name>encodingfilter</filter-name>  
  11. <url-pattern>/*</url-pattern>  
  12. </filter-mapping></pre><br>  
  13. <pre></pre>  
  14. <pre></pre>  
  • 作用

用于指定WEB容器的过滤器,在请求和响应对象在Servlet处理之前和之后,可以通过此过滤器对这两个对象进行处理。

  • 备注

filter-class 中指定的过滤器类须继承 javax.servlet.Filter 具有须有以下三种方法

init(FilterConfig filterConfig):初始化;一般情况下时读取配置文件中的init-param参数值 如 filterConfig.getInitParameter("encoding")

doFilter(...):用于对request,response进行处理,并能过chain.doFilter(...) 交过下一个控制器

destroy():资源销毁

  • 示例:如编码过滤器

web.xml配置

  1. <filter>  
  2. <filter-name>encodingfilter</filter-name>  
  3. <filter-class>com.my.app.EncodingFilter</filter-class>  
  4. <init-param>  
  5. <param-name>encoding</param-name>  
  6. <param-value>UTF-8</param-value>  
  7. </init-param>  
  8. </filter>  
  9. <filter-mapping>  
  10. <filter-name>encodingfilter</filter-name>  
  11. <url-pattern>/*</url-pattern>  
  12. </filter-mapping>  


Java 代码

  1. /** 
  2.  *  
  3.  */  
  4. package com.myapp;  
  5.   
  6. import java.io.IOException;  
  7.   
  8. import javax.servlet.Filter;  
  9. import javax.servlet.FilterChain;  
  10. import javax.servlet.FilterConfig;  
  11. import javax.servlet.ServletException;  
  12. import javax.servlet.ServletRequest;  
  13. import javax.servlet.ServletResponse;  
  14. import javax.servlet.http.HttpServletRequest;  
  15. import javax.servlet.http.HttpServletResponse;  
  16.   
  17. /** 
  18.  * @author louisliao 
  19.  * 
  20.  */  
  21. public class EncodingFilter implements Filter {  
  22.   
  23.     /** 
  24.      * 配置中默认的字符编码 
  25.      */  
  26.     protected String encoding = null;    
  27.     protected FilterConfig filterConfig;  
  28.     /** 
  29.      * 当没有指定默认编码时是否允许跳过过滤 
  30.      */  
  31.     protected boolean ignore = true;   
  32.     /** 
  33.      *  
  34.      */  
  35.     public EncodingFilter() {  
  36.         // TODO Auto-generated constructor stub  
  37.     }  
  38.   
  39.     /* (non-Javadoc) 
  40.      * @see javax.servlet.Filter#destroy() 
  41.      */  
  42.     public void destroy() {  
  43.         // TODO Auto-generated method stub  
  44.         this.encoding=null;  
  45.         this.filterConfig=null;  
  46.     }  
  47.       
  48.     /* (non-Javadoc) 
  49.      * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) 
  50.      */  
  51.     public void doFilter(ServletRequest request, ServletResponse response,  
  52.             FilterChain chain) throws IOException, ServletException {  
  53.         // TODO Auto-generated method stub  
  54.         HttpServletRequest hRequest=(HttpServletRequest)request;  
  55.         HttpServletResponse hResponse=(HttpServletResponse)response;  
  56.         //Conditionally select and set the character encoding to be used    
  57.         if(ignore || hRequest.getCharacterEncoding()==null){  
  58.             String coding=selectEncoding(hRequest);  
  59.             if(coding!=null){  
  60.                 hRequest.setCharacterEncoding(coding);  
  61.                 hResponse.setCharacterEncoding(coding);  
  62.             }  
  63.         }  
  64.         //将控制器传向下一个filter  
  65.         chain.doFilter(hRequest, hResponse);  
  66.   
  67.     }  
  68.   
  69.     /* (non-Javadoc) 
  70.      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) 
  71.      */  
  72.     public void init(FilterConfig filterConfig) throws ServletException {  
  73.         // TODO Auto-generated method stub  
  74.         this.filterConfig=filterConfig;  
  75.         this.encoding=filterConfig.getInitParameter("encoding");  
  76.         System.out.println(this.encoding);  
  77.         String value = filterConfig.getInitParameter("ignore");  
  78.         if (value == null) {  
  79.             this.ignore = true;  
  80.         } else if (value.equalsIgnoreCase("true")) {  
  81.             this.ignore = true;  
  82.         } else if (value.equalsIgnoreCase("yes")) {  
  83.             this.ignore = true;  
  84.         } else {  
  85.             this.ignore = false;  
  86.         }  
  87.     }  
  88.     protected String selectEncoding(ServletRequest request) {  
  89.         return (this.encoding);  
  90.     }  
  91.   
  92. }  
  93. init方法是在WEB应用启动就会调用,doFilter则是在访问filter-mapping映射到的url时会调用。 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值