一、web.xml配置文件常用元素及其意义预览
1 <web-app> 2 3 4 <display-name>display-name> 5 6 7 <description>description> 8 9 10 <context-param>context-param> 11 12 13 <filter>filter> 14 15 16 <filter-mapping>filter-mapping> 17 18 20 <listener>listener> 21 22 24 <servlet>servlet> 25 26 29 <servlet-mapping>servlet-mapping> 30 31 33 <session-config>session-config> 34 35 36 <mime-mapping>mime-mapping> 37 38 39 <welcome-file-list>welcome-file-list> 40 41 42 <error-page>error-page> 43 44 46 <taglib>taglib> 47 48 49 <resource-env-ref>resource-env-ref> 50 51 52 <resource-ref>resource-ref> 53 54 55 <security-constraint>security-constraint> 56 57 58 <login-config>login-config> 59 60 62 <security-role>security-role> 63 64 65 <env-entry>env-entry> 66 67 68 <ejb-ref>ejb-ref> 69 70 71 <ejb-local-ref>ejb-local-ref> 72 73 web-app>
二、各个配置元素详解
1.Web应用图标:指出IDE和GUI工具用来表示Web应用的大图标和小图标
1 <icon> 2 <small-icon>/images/app_small.gifsmall-icon> 3 <large-icon>/images/app_large.giflarge-icon> 4 icon>
2.Web 应用名称:提供GUI工具可能会用来标记这个特定的Web应用的一个名称
<display-name>Tomcat Exampledisplay-name>
3.Web 应用描述:给出于此相关的说明性文本
<desciption>Tomcat Example servlets and JSP pages.desciption>
4.上下文参数:声明应用范围内的初始化参数
1 <context-param> 2 <param-name>参数名para-name> 3 <param-value>参数值param-value> 4 <description>参数描述description> 5 context-param>
在servlet里面可以通过 getServletContext().getInitParameter(“context/param”)得到
5.过滤器配置:将一个名字与一个实现javaxs.servlet.Filter接口的类相关联
1 <filter> 2 <filter-name>setCharacterEncodingfilter-name> 3 <filter-class>com.myTest.setCharacterEncodingFilterfilter-class> 4 <init-param> 5 <param-name>encodingparam-name> 6 <param-value>GB2312param-value> 7 init-param> 8 filter> 9 <filter-mapping> 10 <filter-name>setCharacterEncodingfilter-name> 11 <url-pattern>/*url-pattern> 12 filter-mapping>
6.监听器配置
1 <listener> 2 <listerner-class>org.springframework.web.context.ContextLoaderListenerlistener-class> 3 listener>
7.Servlet配置
1 <servlet> 2 <servlet-name>servlet名称servlet-name> 3 <servlet-class>servlet类全路径servlet-class> 4 <init-param> 5 <param-name>参数名param-name> 6 <param-value>参数值param-value> 7 init-param> 8 <run-as> 9 <description>Security role for anonymous accessdescription> 10 <role-name>tomcatrole-name> 11 run-as> 12 <load-on-startup>指定当Web应用启动时,装载Servlet的次序load-on-startup> 13 servlet> 14 <servlet-mapping> 15 <servlet-name>servlet名称servlet-name> 16 <url-pattern>映射路径url-pattern> 17 servlet-mapping>
8.会话超时配置(单位为分钟)
1 <session-config> 2 <session-timeout>120session-timeout> 3 session-config>
9.MIME类型配置
1 <mime-mapping> 2 <extension>htmextension> 3 <mime-type>text/htmlmime-type> 4 mime-mapping>
10.指定欢迎文件页配置
1 <welcome-file-list> 2 <welcome-file>index.jspwelcome-file> 3 <welcome-file>index.htmlwelcome-file> 4 <welcome-file>index.htmwelcome-file> 5 welcome-file-list>
11.配置错误页面
(1).通过错误码来配置error-page
1 2 <error-page> 3 <error-code>404error-code> 4 <location>/NotFound.jsplocation> 5 error-page>
(2).通过异常的类型配置error-page
1 2 <error-page> 3 <exception-type>java.lang.NullExceptionexception-type> 4 <location>/error.jsplocation> 5 error-page>
12.TLD配置
1 <taglib> 2 <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglibtaglib-uri> 3 <taglib-location>/WEB-INF/jsp/debug-taglib.tldtaglib-location> 4 taglib>
如果开发工具一直在报错,应该把
1 <jsp-config> 2 <taglib> 3 <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglibtaglib-uri> 4 <taglib-location>/WEB-INF/pager-taglib.tldtaglib-location> 5 taglib> 6 jsp-config>
13.资源管理对象配置
1 <resource-env-ref> 2 <resource-env-ref-name>jms/StockQueueresource-env-ref-name> 3 resource-env-ref>
14.资源工厂配置
1 <resource-ref> 2 <res-ref-name>mail/Sessionres-ref-name> 3 <res-type>javax.mail.Sessionres-type> 4 <res-auth>Containerres-auth> 5 resource-ref>
配置数据库连接池就可在此配置
1 <resource-ref> 2 <description>JNDI JDBC DataSource of shopdescription> 3 <res-ref-name>jdbc/sample_dbres-ref-name> 4 <res-type>javax.sql.DataSourceres-type> 5 <res-auth>Containerres-auth> 6 resource-ref>
15.安全限制配置
1 <security-constraint> 2 <display-name>Example Security Constraintdisplay-name> 3 <web-resource-collection> 4 <web-resource-name>Protected Areaweb-resource-name> 5 <url-pattern>/jsp/security/protected/*url-pattern> 6 <http-method>DELETEhttp-method> 7 <http-method>GEThttp-method> 8 <http-method>POSThttp-method> 9 <http-method>PUThttp-method> 10 web-resource-collection> 11 <auth-constraint> 12 <role-name>tomcatrole-name> 13 <role-name>role1role-name> 14 auth-constraint> 15 security-constraint>
16.登陆验证配置
1 <login-config> 2 <auth-method>FORMauth-method> 3 <realm-name>Example-Based Authentiation Arearealm-name> 4 <form-login-config> 5 <form-login-page>/jsp/security/protected/login.jspform-login-page> 6 <form-error-page>/jsp/security/protected/error.jspform-error-page> 7 form-login-config> 8 login-config>
17.安全角色:security-role元素给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。
分别地声明角色可使高级IDE处理安全信息更为容易。
1 <security-role> 2 <role-name>tomcatrole-name> 3 security-role>
18.Web环境参数:env-entry元素声明Web应用的环境项
1 <env-entry> 2 <env-entry-name>minExemptionsenv-entry-name> 3 <env-entry-value>1env-entry-value> 4 <env-entry-type>java.lang.Integerenv-entry-type> 5 env-entry>
19.EJB 声明
1 <ejb-ref> 2 <description>Example EJB referencedecription> 3 <ejb-ref-name>ejb/Accountejb-ref-name> 4 <ejb-ref-type>Entityejb-ref-type> 5 <home>com.mycompany.mypackage.AccountHomehome> 6 <remote>com.mycompany.mypackage.Accountremote> 7 ejb-ref>
20.本地EJB声明
1 <ejb-local-ref> 2 <description>Example Loacal EJB referencedecription> 3 <ejb-ref-name>ejb/ProcessOrderejb-ref-name> 4 <ejb-ref-type>Sessionejb-ref-type> 5 <local-home>com.mycompany.mypackage.ProcessOrderHomelocal-home> 6 <local>com.mycompany.mypackage.ProcessOrderlocal> 7 ejb-local-ref>