web.xml详解

  • 格式:pdf
  • 大小:66.23 KB
  • 文档页数:2

web.xml详解

我们都知web项⽬的运⾏,通常会⾸先加载web.xml⽂件,但却对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的使⽤版本。

web-app是web.xml的根元素,包含着web.xml所有⼦元素。

xmlns以及xmlns:xsi后⾯引进的连接是表明web.xml引进的模式⽂件,便能拥有该模式的相关功能。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

指明项⽬名称

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