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 ...
随机推荐
- codeforces 183B - Zoo
/* 题意:给出n,m. n表示给出的n个横坐标为1-n,y为0的坐标m表示以下有m个坐标,在横坐标上的点 向各个角度看,在可以看到最多的点在同一条直线上的点的做多值为横坐标这一点的值,最后各个 横坐 ...
- css制作的各种图形
1.六角形的制作: 代码: #star-six{ height:0; width:0; border-bottom:100px solid red; border-left: 50px solid t ...
- 使用maven结合requirejs管理前端脚本
已有的web项目,一直使用Maven做工程管理,现阶段前端调整为使用requirejs来负责模块加载依赖,同时使用jasmine来完成前端的UT. 便与在maven下统一管理,简单整理了下合在一起的使 ...
- openWRT自学---对官方的开发指导文档的解读和理解 记录3:一些常用方法
1.约定 configuration files follow the convention: <name>.conf init files follow the convention: ...
- 程序的记事本--log4net
你是否在遇到程序执行问题时常常百度?你是否在遇到执行错误时常常去询问别人?假设有那么是时候改变啦,对于一个Developer来说那是不专业的表现,专业的Developer都会首先查看程序的执行日志.先 ...
- UTF-8和GBK的区别
GBK是在国家标准GB2312基础上扩容后兼容GB2312的标准(好像还不是国家标准).GBK编码专门用来解决中文编码的,是双字节的.不论中英文都是双字节的. UTF-8编码是用以解决国际上字符的一种 ...
- plsql programming 07 使用数据
数据类型 char, Nchar varchar2, Nvarchar2 clob, Nclob number number(9, 2); -- 定点小数, 小数点左边7位, 右边2位 number ...
- IDEA15入门常用设置
打开IDEA设置的快捷键:Ctrl + Alt + S 打开选中的项目属性快捷键:Shift + Ctrl + Alt + S 1.IDEA默认不会使用我们独立安装的Maven配置,需要手动设置,并且 ...
- CentOS 6.5 安装Gitlab 7.12.2
官网环境要求 参见:https://github.com/gitlabhq/gitlabhq GitLab is a Ruby on Rails application that runs on th ...
- Java快车读书笔记
办公自动化:OA 客户关系管理:CRM人力资源:HR 企业资源计划:ERP知识管理:KM 供应链管理:SCM企业设备管理系统:EAM 产品生命周期管理:PLM面向服务体系架构:SOA 商业智能:BI项 ...