一.理论准备html
先说下我记得xml规则,必须有且只有一个根节点,大小写敏感,标签不嵌套,必须配对。java
web.xml是否是必须的呢?不是的,只要你不用到里面的配置信息就行了,不过在大型web工程下使用该文件是很方便的,如果没有也会很复杂。web
那么web.xml能作的全部事情都有那些?其实,web.xml的模式(Schema)文件中定义了多少种标签元素,web.xml中就能够出现它的模式文件所定义的标签元素,它就能拥有定义出来的那些功能。web.xml的模式文件是由Sun公司定义的,每一个web.xml文件的根元素<web-app>中,都必须标明这个web.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" 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">
<display-name>db</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
二.标签元素tomcat
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index1.jsp</welcome-file>
</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的根名。但若是你指定了具体页面,是能够正常访问的。服务器
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>net.test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
url-pattern的意思是全部的.do文件都会通过TestServlet处理。session
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>net.test.TestServlet</servlet-class>
<init-param>
<param-name>userName</param-name>
<param-value>Tommy</param-value>
</init-param>
<init-param>
<param-name>E-mail</param-name>
<param-value>Tommy@163.com</param-value>
</init-param>
</servlet>
通过上面的配置,在servlet中可以调用getServletConfig().getInitParameter("param1")得到参数名对应的值。app
//上下文参数:声明应用范围内的初始化参数。
<context-param>
<param-name>ContextParameter</para-name>
<param-value>test</param-value>
<description>It is a test parameter.</description>
</context-param>
//在servlet里面能够经过getServletContext().getInitParameter("context/param")
获得
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
-----------------------------
<error-page>
<exception-type>java.lang.Exception<exception-type>
<location>/exception.jsp<location>
</error-page>
<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/error.jsp</location>
</error-page>
<filter>
<filter-name>XXXCharaSetFilter</filter-name>
<filter-class>net.test.CharSetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>XXXCharaSetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
6.设置监听器jsp
web.xml中的<listener></listener>有什么用? 没别的用处!就是配置监听类的~,它能捕捉到服务器的启动和中止! 在启动和中止触发里面的方法作相应的操做! 它必须在web.xml 中配置才能使用! web.xml 中listener元素不是只能有一个,有多个时按顺序执行。网站
如何在web.xml向listener中传参数 ?
<listener>
<listener-class>监听器类的完整路径</listener-class>
</listener>
监听器中不可以写初始化参数; 可经过另个的途径达到初始化参数的效果: 1.写一个properties文件,在文件里写好初始化参数值, 2.在监听器中能够通获得properties文件中的值(写在静态块中)。
<session-config>
<session-timeout>60</session-timeout>
</session-config>
除了这些标签元素以外,还能够往web.xml中添加那些标签元素呢,那些标签元素都能起什么做用呢?咱们只要去查看web.xml的模式文件就能知道。直接看模式文件看不懂,能够找一些中文教程来看看。
三.遗留问题