web.xml文件配置说明
web.xml作用:
web.xml主要用来配置Filter、Listener、Servlet等,当我们去启动一个WEB项目时,容器(jetty、tomcat等)首先会读取项目web.xml配置文件里的配置,当这一步骤没有出错并且完成之后,项目才能正常地被启动起来。
web.xml配置元素的加载顺序:
<context-param> -> <listener> -> <filter> -> <servlet>。其中,如果web.xml中出现了相同的元素,则按照在配置文件中出现的先后顺序来加载。
web容器启动过程:
- 启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点。
- 紧急着,容创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文。
- 容器将<context-param>转换为键值对,并交给servletContext。
- 容器创建<listener>中的类实例,创建监听器。
web.xml配置元素:
1.<web-app>根元素
<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.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
</web-app>
2.<icon>Web应用图标
<icon>
<small-icon>/images/app_small.gif</small-icon>
<large-icon>/images/app_large.gif</large-icon>
</icon>
3.<display-name>Web应用名称
<display-name>Tomcat Example</display-name>
4.<disciption>Web应用描述
<disciption>Tomcat Example servlets and JSP pages.</disciption>
5.<context-param>上下文参数
<context-param>
<param-name>ContextParameter</para-name>
<param-value>test</param-value>
<description>It is a test parameter.</description>
</context-param>
6.<filter>过滤器
<filter>
<filter-name>setCharacterEncoding</filter-name>
<filter-class>com.myTest.setCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
7.<listener>监听器
<listener>
<listener-class>com.listener.SessionListener</listener-class>
</listener>
8.<servlet>
<servlet> 用来声明一个servlet的数据,主要有以下子元素:<servlet-name> 指定servlet的名称<servlet-class> 指定servlet的类名称<jsp-file> 指定web站台中的某个JSP网页的完整路径<init-param> 用来定义参数,可有多个init-param。<load-on-startup> 当值为正数或零时,从小到大加载。否则第一次访问时加载。<servlet-mapping> 用来定义servlet所对应的URL,包含两个子元素<servlet-name> 指定servlet的名称<url-pattern> 指定servlet所对应的URL
<servlet>
<servlet-name>snoop</servlet-name>
<servlet-class>SnoopServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>snoop</servlet-name>
<url-pattern>/snoop</url-pattern>
</servlet-mapping>
9.<session-config>会话超时配置
<session-config>
<session-timeout>120</session-timeout><!-- 单位为分钟 -->
</session-config>
10.<mime-mapping>
<mime-mapping>
<extension>htm</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
11.<welcome-file-list>欢迎文件页
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
12.<error-page>错误页面
<!-- 1、通过错误码来配置error-page。当系统发生×××错误时,跳转到错误处理页面。 -->
<error-page>
<error-code>404</error-code>
<location>/NotFound.jsp</location>
</error-page>
<!-- 2、通过异常的类型配置error-page。(即空指针异常)时,跳转到错误处理页面。 -->
<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/error.jsp</location>
</error-page>
spring在web.xml中的配置:
<!-- Spring -->
<!-- 配置Spring配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/applicationContext.xml
</param-value>
</context-param>
<!-- 配置Spring上下文监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring -->
其中监听器ContextLoaderListener是必须的,作用是加载spring的配置文件,如果不配置contextConfigLocation,则默认的路径是/WEB-INF/applicationContext.xml。
application的相关配置介绍:applicationContext.xml配置简介
spring MVC在web.xml中的配置:
<!-- Spring MVC配置 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value> 默认
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
springMVC配置的相关介绍:Spring MVC学习笔记
WebAppRootListener在web.xml中的配置:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
WebAppRootListener用于获得项目根目录,在代码中使用System.getProperty(webapp.root)获得。
WebAppRootListener要在ApplicationContext的ContextLoaderListener之前,否则ApplicationContext的bean注入根目录值时会发生无法注入异常。
但是如果在web.xml中已经配置了 org.springframework.web.util.Log4jConfigListener 这个监听器,则不需要配置WebAppRootListener了。因为Log4jConfigListener已经包含了WebAppRootListener的功能。
参考:
web.xml文件配置说明的更多相关文章
- web.xml文件加载顺序
1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Servl ...
- Java Web的web.xml文件作用及基本配置(转)
其实web.xml就是asp.net的web.config一个道理. 说明: 一个web中完全可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的. web.xml文件是用来 ...
- web.xml文件详解
web.xml文件详解 Table of Contents 1 listener. filter.servlet 加载顺序 2 web.xml文件详解 3 相应元素配置 1 listener. f ...
- web.xml 文件配置01
web.xml 文件配置01 前言:一般的web工程中都会用到web.xml,方便开发web工程.web.xml主要用来配置Filter.Listener.Servlet等.但是要说明的是web. ...
- web.xml文件中的web-app元素 部署
[转载]web.xml文件中的web-app元素 (2012-05-24 13:35:57) 转载▼ 标签: 转载 分类: java 挺全 的 呵呵呵 转了 原文地址:web.xml文件中的web-a ...
- WEB项目web.xml文件中classpath: 跟classpath*:使用的区别
引用一篇很不错的文章:http://blog.csdn.net/wxwzy738/article/details/16983935 首先 classpath是指 WEB-INF文件夹下的classes ...
- web.xml文件中加载顺序的优先级
在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...
- web.xml文件的作用
每个javaEE工程中都有web.xml文件,那么它的作用是什么呢?它是每个web.xml工程都必须的吗? 一个web中可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的. ...
- web.xml文件中配置ShallowEtagHeaderFilter需注意的问题
问题现象及解决方法 今天在Spring MVC应用中加入ShallowEtagHeaderFilter时,发现返回的响应中没有etag头,上网查了很多相关资料,也试了很多方法依然不起作用.在查看web ...
随机推荐
- python模块学习之collections
更多信息请前往官网网址: https://docs.python.org/3.6/library/collections.html 8.3.5. namedtuple() 命名字段的元组的工场函数 命 ...
- php类库PHP QR Code 二维码
php类库PHP QR Code 二维码 php类库PHP QR Code 二维码 php类库PHP QR CodePHP QR Code is open source (LGPL) library ...
- oracle中查看sql语句的执行计划
1.在pl/sql中打开cmd命令容器 2.在cmd命令窗口中输入:explain plan for select * from t; 3.查看sql语句的执行计划:select * from tab ...
- C语言结构体指针的引用问题
在写栈的一个应用时遇见这样的一个问题 SqStack s; s->base = (int*)malloc(sizeof(int)*10); 通过这样一个代码引用的时候,会导致程序出现异常 经过一 ...
- IOS设计模式浅析之适配器模式(Adapter)
引言 在项目开发中,有时候会遇到这样的一种情景:需要使用以前开发的“一些现存的对象”,但是新环境中要求的接口是这些现存对象所不满足的.怎样应对这种迁移的需求?使得可以复用这些对象,以满足新的应用环境, ...
- LeetCode406. Queue Reconstruction by Height Add to List
Description Suppose you have a random list of people standing in a queue. Each person is described b ...
- form表单提交方式
form表单提交方式总结一下: 一.利用submit按钮实现提交,当点击submit按钮时,触发onclick事件,由JavaScript里函数判断输入内容是否为空,如果为空,返回false, 不提交 ...
- linux 分区格式查看
Linux分区格式查看 两个文件 /etc/fstab 和/etc/mtab /etc/fstab是用来存放文件系统的静态信息的文件,当系统启动的时候. 系统会自动地从这个文件读取信息,并且会自动将此 ...
- WPF 后台任务 等待动画 样例 && C# BackgroundWorker 详解
运行效果: 前台代码: <Window x :Class="Waiting.Window1" xmlns="http://schemas.microsoft.com ...
- c++中sin,cos,arcsin等和在C/C++中使用pi (π) 值
先 #include<math.h> 反3角函数有 acos(double),asin(double),atan(double),atan(double,double),返回值 doubl ...