Web.XML 配置学习

web.xml的作用(参考资料):https://www.cnblogs.com/yqskj/articles/2233061.html

每个javaEE工程中都有web.xml文件,那么它的作用是什么呢?它是每个web.xml工程都必须的吗?

一个web中可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的。

web.xml文件是用来初始化配置信息:比如Welcome页面、servlet、servlet-mapping、filter、listener、启动加载级别等。

当你的web工程没用到这些时,你可以不用web.xml文件来配置你的Application。

每个xml文件都有定义它书写规则的Schema文件,也就是说javaEE的定义web.xml所对应的xml Schema文件中定义了多少种标签元素,web.xml中就可以出现它所定义的标签元素,也就具备哪些特定的功能。web.xml的模式文件是由Sun 公司定义的,每个web.xml文件的根元素为<web-app>中,必须标明这个web.xml使用的是哪个模式文件

web.xml加载过程参考资料:https://blog.csdn.net/ahou2468/article/details/79015251                                         https://www.cnblogs.com/vanl/p/5737656.html

首先简单讲一下,web.xml的加载过程。当启动一个WEB项目时,容器包括(JBoss、Tomcat等)首先会读取项目web.xml配置文件里的配置,当这一步骤没有出错并且完成之后,项目才能正常地被启动起来。

1. 启动WEB项目的时候,容器首先会去它的配置文件web.xml读取两个节点:  <listener></listener>和<context-param></context-param>

2. 紧接着,容器创建一个ServletContext(application),这个WEB项目所有部分都将共享这个上下文

3. 容器以<context-param></context-param>的name作为键,value作为值,将其转化为键值对,存入ServletContext

4. 容器创建<listener></listener>中的类实例,根据配置的class类路径<listener-class>来创建监听,在监听中会有contextInitialized(ServletContextEvent args)初始化方法,启动Web应用时,系统调用Listener的该方法,在这个方法中获得

context-param的值就是application.getInitParameter("context-param的键");得到这个context-param的值之后,你就可以做一些操作了

5. 举例:你可能想在项目启动之前就打开数据库,那么这里就可以在<context-param>中设置数据库的连接方式(驱动、url、user、password),在监听类中初始化数据库的连接。这个监听是自己写的一个类,除了初始化方法,它还有销毁方法,用于关闭应用前释放资源。比如:说数据库连接的关闭,此时,调用contextDestroyed(ServletContextEvent args),关闭Web应用时,系统调用Listener的该方法

6. 接着,容器会读取<filter></filter>,根据指定的类路径来实例化过滤器

7. 以上都是在WEB项目还没有完全启动起来的时候就已经完成了的工作。如果系统中有Servlet,则Servlet是在第一次发起请求的时候被实例化的,而且一般不会被容器销毁,它可以服务于多个用户的请求。所以,Servlet的初始化都要比上面提到的那几个要迟

8. 总的来说,web.xml的加载顺序是:<context-param>-><listener>-><filter>-><servlet>。其中,如果web.xml中出现了相同的元素,则按照在配置文件中出现的先后顺序来加载

9. 对于某类元素而言,与它们出现的顺序是有关的。以<filter>为例,web.xml中当然可以定义多个<filter>,与<filter>相关的一个元素是<filter-mapping>,注意,对于拥有相同<filter-name>的<filter>和<filter-mapping>元素而言,<filter-mapping>必须出现在<filter>之后,否则当解析到<filter-mapping>时,它所对应的<filter-name>还未定义。web容器启动初始化每个<filter>时,按照<filter>出现的顺序来初始化的,当请求资源匹配多个<filter-mapping>时,<filter>拦截资源是按照<filter-mapping>元素出现的顺序来依次调用doFilter()方法的。<servlet>同<filter>类似,此处不再赘述

web.xml配置,springmvc.xml和applicationContext.xml一起引入

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <!-- 配置监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>    <!-- 引入applicationContext.xml -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- 中央控制器 -->    <!-- 引入spring-mvc.xml -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <!-- 配置Spring提供的字符编码过滤器 -->
    <filter>
        <filter-name>SpringCharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>SpringCharacterEncodingFilter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>
</web-app>

原文地址:https://www.cnblogs.com/lindaiyu/p/10926025.html

时间: 2024-08-28 12:02:00

Web.XML 配置学习的相关文章

基于注解的Spring MVC(所需jar包,web.xml配置,Spring文件配置,@Controller,@RequestMapping,@RequestParam,model填參,EL取值)

1.加入jar 2.web.xml配置: <?xml version="1.0" encoding="UTF-8"? > <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocati

springmvc web.xml配置之SpringMVC IOC容器初始化

SpringMVC IOC容器初始化 首先强调一下SpringMVC IOC容器初始化有些特别,在SpringMVC中除了生成一个全局的spring Ioc容器外,还会为DispatcherServlet生成一个容器,具体的下一篇有讲述. 我们知道spring中单纯使用main函数就可以生成一个容器,如下: public class MainTest { public static void main(String[] args){ ApplicationContext appContext =

web.xml 配置applicationContext.xml

web.xml中classpath:和classpath*:  有什么区别? classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找. 有时候会用模糊匹配的方式配置多配置文件. 但是如果配置文件是在jar包里,模糊匹配就找不到了.可以用逗号隔开的方式配置多个配置文件. 如: <listener>  <listener-class>org.springframework.web.conte

Spring MVC的web.xml配置详解(转)

出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在web.xml配置监听器ContextLoaderListener(listener-class) ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web.

maven工程web层的web.xml配置文档内容

下面是web层,web.xml配置文档里面需要配置的东西: 1.lo4j配置 2.读取spring文件配置 3.设计路径变量值 4.spring字符集过滤器 5.登陆过滤器 6.springMVC核心配置 7.session过期时间 8.错误页面跳转 以下是实例: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSch

struts2拦截器-自定义拦截器,放行某些方法(web.xml配置)

一.web.xml配置 <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-val

SSH框架 web.xml配置

<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.s

web.xml配置整理

虽然是做web开发,但是web中的很多配置有的时候却不是很清楚,只是知道怎么配置,于是就把在网上看到各种关于web.xml的东西整理一下: web.xml中url-pattern的3种写法 1完全匹配 <url-pattern>/test/list.do</url-pattern> 2 路径匹配 <url-pattern>/*</url-pattern> <!--匹配根路径下的全部请求--> 3扩展名匹配 <url-pattern>*

Web.xml配置详解(转)

Web.xml配置详解 Posted on 2010-09-02 14:09 chinaifne 阅读(295105) 评论(16) 编辑 收藏 1 定义头和根元素 部署描述符文件就像所有XML文件一样,必须以一个XML头开始.这个头声明可以使用的XML版本并给出文件的字符编码.DOCYTPE声明必须立即出现在此头之后.这个声明告诉服务器适用的servlet规范的版本(如2.2或2.3)并指定管理此文件其余部分内容的语法的DTD(Document Type Definition,文档类型定义).