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 ...
随机推荐
- Linux Tools
WinSCP http://winscp.net/eng/download.php Xshell 5
- JAVASCRIPT中{} + {}的结果是什么?
转自:http://www.heyria.com/index.php/2014/01/js-object-plus-object/ 当对象或者数组相加的时候,会产生有点意外的结果. 这篇文章主要是解释 ...
- pthread_cleanup_push和pthread_cleanup_pop清除函数是否执行的说明
示例1: #include <stdio.h> #include <pthread.h> void* clean(void* arg) { printf("clean ...
- 矩阵中的路径 剑指offer65题
include "stdafx.h" #include<vector> #include<algorithm> #include<string> ...
- Spring Java-based容器配置(二)
组装Java-based的配置 使用@Import注解 跟在Spring XML文件里使用<import>元素加入模块化的配置相似,@Import注解同意你载入其它配置类中的@Bean定义 ...
- plsql programming 14 DML和事务管理
我们可以把多个SQL语句集中在一起, 在逻辑上组成一个事务, 从而保证这些操作或者全部被保存到数据库(用sql的说法就是”提交”), 或者被整体驳回(用sql的说法是“回滚”). 事务: ACID 原 ...
- redis的下载
网址一:https://github.com/dmajkic/redis/downloads 网址二:http://windows.php.net/downloads/pecl/releases/re ...
- 这篇文章主要为大家详细介绍了jQuery密码强度验证控件使用详解的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了jQuery密码强度验证控件,供大家参考,具体内容如下 <html> <head> <meta http-equiv="Cont ...
- Collection Set List 集合二
Set List 都继承Collection Collection:元素之间没有顺序,允许重复和多个null元素对象. Set:元素之间没有顺序,不允许重复只能存一个null. List:元素之间有顺 ...
- Linux C 获取系统时间信息
比如获取当前年份: /* 获取当前系统时间 暂时不使用 ; ; time_t now; struct tm *timenow; time(&now); timeno ...