web.xml是什么?


一.web.xml是什么?

web.xml是web项目的配置文件,一般的web工程都会用到web.xml来配置,主要用来配置Listener,Filter,Servlet等。

但需要注意的是:web.xml并不是必须的,一个web工程可以没有web.xml文件

二.web项目加载web.xml过程

Tomcat启动web项目时,web容器就会的加载首先加载web.xml文件,

加载过程如下:

1.web项目加载web.xml,读取context-param和listener这两个结点,

2.创建一个ServletContext(Servlet上下文),整个项目会共享这个ServletContext

3.容器将转换为键值对,并交给ServletContext

4.容器创建中的类实例,创建监听器

三.web.xml元素详解

首先是表明xml的使用版本。

<?xml version="1.0" encoding="UTF-8"?>
web-app是web.xml的根元素,包含着web.xml所有子元素。
xmlns以及xmlns:xsi后面引进的连接是表明web.xml引进的模式文件,便能拥有该模式的相关功能。


指明项目名称
web-SSMTest

web项目加载web.xml首先读取这两个结点,加载spring容器及创建spring监听器;

ApplicationContext.xml 是spring 全局配置文件,用来控制spring 特性的

ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。

  
    contextConfigLocation
    classpath:spring/applicationContext.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
这个过滤器就是针对于每次浏览器请求进行过滤的
  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
  
  
    encodingFilter
    /*
  
配置DispatcherServlet 前端控制器,加载springMVC容器
  
    SpringMVC
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:spring/springMVC.xml
    
    1
    true
  
  
    SpringMVC
    /
  
展示首页页面
  
    index.html
  
session配置 
  
    15