Zach的博客

Web.xml标签记录

Web.xml 标签记录

初始化过程

  1. 在启动Web项目时,WEB容器会去读取它的配置文件web.xml,读取两个节点。
  2. 紧接着,容器创建一个ServletContext实例,这个web项目共享这个servletContext实例
  3. 容器将转换维键值对,并交给servletContext。
  4. 容器创建中的类实例,创建监听器

加载的顺序为和标签在文件中出现的顺序是没有关系的(对不同类标签而言),加载的顺序为

1
context-param --> listener --> filter --> servlet

对某类配置节点而言,它们的加载顺序和出现顺序有关。例如若若多个都映射到了同一个url,那么按照出现的顺序依次做doFilter

Context-param

<Context-param>用来声明应用范围内的上下文初始化参数,一个例子如下

1
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>contextConfgiLocationValue</param-value>
</context-param>
  1. <param-name> 用于声明参数名
  2. <param-value> 用于声明参数值

而后在Servlet中可以通过以下方式得到初始化参数

1
String paramValue = getServletContext().getInitParameter("contextConfigLocation")

listener

<listener>用于注册一个监听器类,当一个事件(在建立、修改和删除会话或者servlet环境)发生时得到通知,可以对时间做出相应的响应。其常与<context-param>一起使用。

示例如下:

1
<!--初始化日志配置文件 -->  
<listener>  
    <listener-class>  
        com.myapp.LogbackConfigListener  
    </listener-class>  
</listener>  
<context-param>  
    <param-name>logbackConfigLocation</param-name>  
    <param-value>WEB-INF/logback.xml</param-value>  
</context-param>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

public class LogbackConfigListener implements ServletContextListener {

private static final Logger logger = LoggerFactory.getLogger(LogbackConfigListener.class);
private static final String CONFIG_LOCATION = "logbackConfigLocation";

public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub

}
public void contextInitialized(ServletContextEvent event) {
// TODO Auto-generated method stub
String logbackConfigLocation = event.getServletContext().getInitParameter(CONFIG_LOCATION);
String fn = event.getServletContext().getRealPath(logbackConfigLocation);
try {
LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
loggerContext.reset();
JoranConfigurator joranConfigurator = new JoranConfigurator();
joranConfigurator.setContext(loggerContext);
joranConfigurator.doConfigure(fn);
logger.debug("loaded slf4j configure file from {}", fn);
}
catch (JoranException e) {
logger.error("can loading slf4j configure file from " + fn, e);
}

}

}

filter

<filter>用于在请求和响应对象被servlet处理之前或者之后,使用过滤器对象对这两个对象进行操作。<filter-class>中指定的过滤器必须继承以下三种方法:

  • init(FilterConfig filterConfig)
  • doFilter(…): 用于对request,response进行处理,并能通过chain.doFilter(…)将控制权交给下一个过滤器
  • destroy(): 资源销毁

一个用于设置编码的filter配置示例:

1
<filter>
  <filter-name>setCharacterEncoding</filter-name>
  <filter-class>coreservlet.javaworld.CH11.SetCharacterEncodingFilter</filter-class>
  <init-param>
     <param-name>encoding</param-name>
     <param-value>GB2312</param-value>
  </init-param>
</filter>
<filter-mapping>
   <filter-name>setCharacterEncoding</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

servlet

用于配置servlet所对应的URL

session-config

<session-config>用于定义session的有效期限,单位为分钟

mime-mapping

<mime-mapping>用于定于扩展名(extension)与MIME类型的对映,如:

1
<mime-mapping>  
   <extension>doc</extension>  
   <mime-type>application/vnd.ms-word</mime-type>  
</mime-mapping>

welcome-file-list与error-page

<welcome-file-list>用于指定WEB应用首页,<erro-page>用于指定对应的错误代码(如404)或者java异常(如java.lang.Exception)对应的页面

jsp-config

<jsp-config>主要用于设定JSP页面的相关配置。

一个较为完整的配置如下:

1
<jsp-config>
    <taglib>
        <taglib-uri>http://mytaglib.uri</taglib-uri>
        <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
    </taglib>
    <jsp-property-group>
        <description>Special property group for JSP Configuration JSP example.</description>
        <display-name>JSPConfiguration</display-name>
        <url-pattern>/jsp/* </url-pattern>
        <el-ignored>true</el-ignored>
        <page-encoding>GB2312</page-encoding>
        <scripting-invalid>true</scripting-invalid>
        <include-prelude>/include/prelude.jspf</include-prelude>
        <include-coda>/include/coda.jspf</include-coda>
    </jsp-property-group>
</jsp-config>