一.理论准备html
先说下我记得xml规则,必须有且只有一个根节点,大小写敏感,标签不嵌套,必须配对。java
web.xml是否是必须的呢?不是的,只要你不用到里面的配置信息就行了,不过在大型web工程下使用该文件是很方便的,如果没有也会很复杂。web
那么web.xml能作的全部事情都有那些?其实,web.xml的模式(Schema)文件中定义了多少种标签元素,web.xml中就能够出现它的模式文件所定义的标签元素,它就能拥有定义出来的那些功能。web.xml的模式文件是由Sun公司定义的,每一个web.xml文件的根元素<web-app>中,都必须标明这个web.xml使用的是哪一个模式文件。
来看个例子:spring
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <display-name>db</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 </web-app>
二.标签元素浏览器
1 <welcome-file-list> 2 <welcome-file>index.jsp</welcome-file> 3 <welcome-file>index1.jsp</welcome-file> 4 </welcome-file-list>
上面的例子指定了2个欢迎页面,显示时按顺序从第一个找起,若是第一个存在,就显示第一个,后面的不起做用。若是第一个不存在,就找第二个,以此类推。
关于欢迎页面:访问一个网站时,默认看到的第一个页面就叫欢迎页,通常状况下是由首页来充当欢迎页的。通常状况下,咱们会在web.xml中指定欢迎页。但web.xml并非一个Web的必要文件,没有web.xml,网站仍然是能够正常工做的。只不过网站的功能复杂起来后,web.xml的确有很是大用处,因此,默认建立的动态web工程在WEB-INF文件夹下面都有一个web.xml文件。
对于tomcat来讲,当你只指定一个web的根名,没有指定具体页面,去访问时一个web时,若是web.xml文件中配置了欢迎页,那么就返回指定的那个页面做为欢迎页,而在文中没有web.xml文件,或虽然有web.xml,但web.xml也没指定欢迎页的状况下,它默认先查找index.html文件,若是找到了,就把index.html做为欢迎页还回给浏览器。若是没找到index.html,tomcat就去找index.jsp。找到index.jsp就把它做为欢迎页面返回。而若是index.html和index.jsp都没找到,又没有用web.xml文件指定欢迎页面,那此时tomcat就不知道该返回哪一个文件了,它就显示The requested resource (/XXX) is not available(我就出现过这个问题)的页面。其中XXX表示web的根名。但若是你指定了具体页面,是能够正常访问的。tomcat
1 <servlet> 2 <servlet-name>servlet1</servlet-name> 3 <servlet-class>net.test.TestServlet</servlet-class> 4 </servlet> 5 6 <servlet-mapping> 7 <servlet-name>servlet1</servlet-name> 8 <url-pattern>*.do</url-pattern> 9 </servlet-mapping>
url-pattern的意思是全部的.do文件都会通过TestServlet处理。服务器
1 <servlet> 2 <servlet-name>servlet1</servlet-name> 3 <servlet-class>net.test.TestServlet</servlet-class> 4 <init-param> 5 <param-name>userName</param-name> 6 <param-value>Tommy</param-value> 7 </init-param> 8 <init-param> 9 <param-name>E-mail</param-name> 10 <param-value>Tommy@163.com</param-value> 11 </init-param> 12 </servlet>
通过上面的配置,在servlet中可以调用getServletConfig().getInitParameter("param1")得到参数名对应的值。session
1 //上下文参数:声明应用范围内的初始化参数。 2 <context-param> 3 <param-name>ContextParameter</para-name> 4 <param-value>test</param-value> 5 <description>It is a test parameter.</description> 6 </context-param> 7 //在servlet里面能够经过getServletContext().getInitParameter("context/param")
获得
1 <error-page> 2 <error-code>404</error-code> 3 <location>/error404.jsp</location> 4 </error-page> 5 ----------------------------- 6 <error-page> 7 <exception-type>java.lang.Exception<exception-type> 8 <location>/exception.jsp<location> 9 </error-page> 10 <error-page> 11 <exception-type>java.lang.NullException</exception-type> 12 <location>/error.jsp</location> 13 </error-page>
1 <filter> 2 <filter-name>XXXCharaSetFilter</filter-name> 3 <filter-class>net.test.CharSetFilter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>XXXCharaSetFilter</filter-name> 7 <url-pattern>/*</url-pattern> 8 </filter-mapping>
6.设置监听器app
web.xml中的<listener></listener>有什么用? 没别的用处!就是配置监听类的~,它能捕捉到服务器的启动和中止! 在启动和中止触发里面的方法作相应的操做! 它必须在web.xml 中配置才能使用! web.xml 中listener元素不是只能有一个,有多个时按顺序执行。jsp
如何在web.xml向listener中传参数 ?
1 <listener> 2 <listener-class>监听器类的完整路径</listener-class> 3 </listener>
监听器中不可以写初始化参数; 可经过另个的途径达到初始化参数的效果: 1.写一个properties文件,在文件里写好初始化参数值, 2.在监听器中能够通获得properties文件中的值(写在静态块中)。
1 <session-config> 2 <session-timeout>60</session-timeout> 3 </session-config>
除了这些标签元素以外,还能够往web.xml中添加那些标签元素呢,那些标签元素都能起什么做用呢?咱们只要去查看web.xml的模式文件就能知道。直接看模式文件看不懂,能够找一些中文教程来看看。
三.遗留问题
四.参考资料
csdn:http://blog.csdn.net/shanliangliuxing/article/details/7458492
原文连接
https://www.cnblogs.com/hxsyl/p/3435412.html
SpringMVC核心分发器DispatcherServlet分析[附带源码分析]
https://www.cnblogs.com/fangjian0423/p/springMVC-dispatcherServlet.html