一、web.xml配置文件常用元素及其意义预览

 <web-app>

      <!--定义了WEB应用的名字-->
<display-name></display-name> <!--声明WEB应用的描述信息-->
<description></description> <!--context-param元素声明应用范围内的初始化参数-->
<context-param></context-param> <!--过滤器元素将一个名字与一个实现javax.servlet.Filter接口的类相关联-->
<filter></filter> <!--一旦命名了一个过滤器,就要利用filter-mapping元素把它与一个或多个servlet或JSP页面相关联-->
<filter-mapping></filter-mapping> <!--servlet API的版本2.3增加了对事件监听程序的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知。
Listener元素指出事件监听程序类-->
<listener></listener> <!--在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面。
Servlet元素就是用来完成此项任务的-->
<servlet></servlet> <!--服务器一般为servlet提供一个缺省的URL:http://host/webAppPrefix/servlet/ServletName。
但是,常常会更改这个URL,以便servlet可以访问初始化参数或更容易地处理相对URL。
在更改缺省URL时,使用servlet-mapping元素-->
<servlet-mapping></servlet-mapping> <!--如果某个会话在一定时间内未被访问,服务器可以抛弃它以节省内存。可通过使用HttpSession的
setMaxInactiveInterval方法明确设置单个会话对象的超时值,或者可利用session-config元素制定缺省超时值-->
<session-config></session-config> <!--如果Web应用具有想到特殊的文件,希望能保证给他们分配特定的MIME类型,则mime-mapping元素提供这种保证-->
<mime-mapping></mime-mapping> <!--指示服务器在收到引用一个目录名而不是文件名的URL时,使用哪个文件-->
<welcome-file-list></welcome-file-list> <!--在返回特定HTTP状态代码时,或者特定类型的异常被抛出时,能够制定将要显示的页面-->
<error-page></error-page> <!--对标记库描述符文件(Tag Libraryu Descriptor file)指定别名。此功能使你能够更改TLD文件的位置,
而不用编辑使用这些文件的JSP页面-->
<taglib></taglib> <!--声明与资源相关的一个管理对象-->
<resource-env-ref></resource-env-ref> <!--声明一个资源工厂使用的外部资源-->
<resource-ref></resource-ref> <!--制定应该保护的URL。它与login-config元素联合使用-->
<security-constraint></security-constraint> <!--指定服务器应该怎样给试图访问受保护页面的用户授权。它与sercurity-constraint元素联合使用-->
<login-config></login-config> <!--给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。
分别地声明角色可使高级IDE处理安全信息更为容易-->
<security-role></security-role> <!--声明Web应用的环境项-->
<env-entry></env-entry> <!--声明一个EJB的主目录的引用-->
<ejb-ref></ejb-ref> <!--声明一个EJB的本地主目录的应用-->
<ejb-local-ref></ejb-local-ref> </web-app>

二、各个配置元素详解

1.Web应用图标:指出IDE和GUI工具用来表示Web应用的大图标和小图标

 <icon>
<small-icon>/images/app_small.gif</small-icon>
<large-icon>/images/app_large.gif</large-icon>
</icon>

2.Web 应用名称:提供GUI工具可能会用来标记这个特定的Web应用的一个名称

<display-name>Tomcat Example</display-name>

3.Web 应用描述:给出于此相关的说明性文本

<desciption>Tomcat Example servlets and JSP pages.</desciption>

4.上下文参数:声明应用范围内的初始化参数

 <context-param>
<param-name>参数名</para-name>
<param-value>参数值</param-value>
<description>参数描述</description>
</context-param>

在servlet里面可以通过 getServletContext().getInitParameter(“context/param”)得到

5.过滤器配置:将一个名字与一个实现javaxs.servlet.Filter接口的类相关联

 <filter>
<filter-name>setCharacterEncoding</filter-name>
<filter-class>com.myTest.setCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

6.监听器配置

 <listener>
<listerner-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

7.Servlet配置

 <servlet>
<servlet-name>servlet名称</servlet-name>
<servlet-class>servlet类全路径</servlet-class>
<init-param>
<param-name>参数名</param-name>
<param-value>参数值</param-value>
</init-param>
<run-as>
<description>Security role for anonymous access</description>
<role-name>tomcat</role-name>
</run-as>
  <load-on-startup>指定当Web应用启动时,装载Servlet的次序</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servlet名称</servlet-name>
<url-pattern>映射路径</url-pattern>
</servlet-mapping>

8.会话超时配置(单位为分钟)

 <session-config>
<session-timeout>120</session-timeout>
</session-config>

9.MIME类型配置

 <mime-mapping>
<extension>htm</extension>
<mime-type>text/html</mime-type>
</mime-mapping>

10.指定欢迎文件页配置

  <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>

11.配置错误页面

  (1).通过错误码来配置error-page

 <!--配置了当系统发生404错误时,跳转到错误处理页面NotFound.jsp-->
<error-page>
<error-code>404</error-code>
<location>/NotFound.jsp</location>
</error-page>

  (2).通过异常的类型配置error-page

 <!--配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp-->
<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/error.jsp</location>
</error-page>

12.TLD配置

 <taglib>
<taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
<taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>
</taglib>

如果开发工具一直在报错,应该把<taglib> 放到 <jsp-config>中

 <jsp-config>
<taglib>
<taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
<taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>
</taglib>
</jsp-config>

13.资源管理对象配置

 <resource-env-ref>
<resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
</resource-env-ref>

14.资源工厂配置

 <resource-ref>
<res-ref-name>mail/Session</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>

  配置数据库连接池就可在此配置

  <resource-ref>
<description>JNDI JDBC DataSource of shop</description>
<res-ref-name>jdbc/sample_db</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

15.安全限制配置

 <security-constraint>
<display-name>Example Security Constraint</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/jsp/security/protected/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>tomcat</role-name>
<role-name>role1</role-name>
</auth-constraint>
</security-constraint>

16.登陆验证配置

  <login-config>
<auth-method>FORM</auth-method>
<realm-name>Example-Based Authentiation Area</realm-name>
<form-login-config>
<form-login-page>/jsp/security/protected/login.jsp</form-login-page>
<form-error-page>/jsp/security/protected/error.jsp</form-error-page>
</form-login-config>
</login-config>

17.安全角色:security-role元素给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。
分别地声明角色可使高级IDE处理安全信息更为容易。

 <security-role>
<role-name>tomcat</role-name>
</security-role>

18.Web环境参数:env-entry元素声明Web应用的环境项

 <env-entry>
<env-entry-name>minExemptions</env-entry-name>
<env-entry-value>1</env-entry-value>
<env-entry-type>java.lang.Integer</env-entry-type>
</env-entry>

19.EJB 声明

 <ejb-ref>
<description>Example EJB reference</decription>
<ejb-ref-name>ejb/Account</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>com.mycompany.mypackage.AccountHome</home>
<remote>com.mycompany.mypackage.Account</remote>
</ejb-ref>

20.本地EJB声明

  <ejb-local-ref>
<description>Example Loacal EJB reference</decription>
<ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>
<local>com.mycompany.mypackage.ProcessOrder</local>
</ejb-local-ref>

三、总结

  以上就是常用的web.xml中元素的配置以及作用了,欢迎提出异议和不适当的地方共同学习。

史上最全web.xml配置文件元素详解的更多相关文章

  1. web.xml配置文件元素详解

    一.web.xml配置文件常用元素及其意义 1 <web-app> 2 3 <!--定义了WEB应用的名字--> 4 <display-name></disp ...

  2. web.xml常用配置详解

    web.xml常用配置详解 context-param 指定 ServletContext(上下文) 配置文件路径,基本配置一般是Spring配置文件,或者是spring-security的配置文件. ...

  3. ssh2的application.xml配置文件配置详解

    ssh2的application.xml配置文件配置详解   1.导入其他的配置文件.在ssh项目中可以导入其他的配置文件,导入的格式为: <import resource="clas ...

  4. web.xml中context-param详解

    <context-param> <param-name>contextConfigLocation</param-name> <param-value> ...

  5. 新手入门:史上最全Web端即时通讯技术原理详解

    前言 有关IM(InstantMessaging)聊天应用(如:微信,QQ).消息推送技术(如:现今移动端APP标配的消息推送模块)等即时通讯应用场景下,大多数都是桌面应用程序或者native应用较为 ...

  6. Web.xml配置参数详解

    1 定义头和根元素 部署描述符文件就像所有XML文件一样,必须以一个XML头开始.这个头声明可以使用的XML版本并给出文件的字符编码.DOCYTPE声明必须立即出现在此头之后.这个声明告诉服务器适用的 ...

  7. WEB容器启动——web.xml加载详解

    最近在看spring的源码,关于web.xml文件在容器(Tomcat.JBOSS等)启动时加载顺序问题很混乱,通过搜集资料,得出以下的结论: 1.加载顺序与它们在 web.xml 文件中的先后顺序无 ...

  8. Servlet中Web.xml的配置详解

    1 定义头和根元素 部署描述符文件就像所有XML文件一样,必须以一个XML头开始.这个头声明可以使用的XML版本并给出文件的字符编码. DOCYTPE声明必须立即出现在此头之后.这个声明告诉服务器适用 ...

  9. servlet web.xml配置选项详解

    一般的web工程中都会用到web.xml,web.xml主要包括一些配置标签,例如Filter.Listener.Servlet等,可以用来预设容器的配置,可以方便的开发web工程.但是web.xml ...

随机推荐

  1. POJ 2635 The Embarrassed Cryptographer

    大数取MOD... The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1 ...

  2. PHP函数 rtrim() 的一个怪异现象

    今天用rtrim()函数时遇到了一个奇怪的问题: echo rtrim('<p></div>', '</div>'); // 输出为 <p echo ltri ...

  3. php远程连接http方式

    以下这三者是通过GET方式来获取数据 1.socket 方式 一般是指定网址.端口号.超时时间. 再对报头进行修改以及传递参数,包括:header.method.content, 返回的内容包括hea ...

  4. mysql-mysql悲观锁和乐观锁

    1.mysql的四种事务隔离级别 I. 对于同时运行多个事务,当这些事务访问数据库中的相同数据时,如果没有采取必要的隔离机制,就会导致各种并发问题. (1)脏读: 对于两个事物 T1, T2, T1 ...

  5. hibernate之persistence错误

    错误码: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityM ...

  6. .Net的要知道的一些事

    1.什么是.NET?什么是CLI?什么是CLR?IL是什么?JIT是什么,它是如何工作的?GC是什么,简述一下GC的工作方式? .Net是微软推出的框架 CLI是公共语言接口(规范) CLR是公共语言 ...

  7. iOS开发——UI基础-UIScrollView

    一.UIScrollView使用的步骤 1.创建UIScrollView 2.将需要展示的内容添加到UIScrollView中 3.设置UIScrollView的滚动范围 (contentSize) ...

  8. BZOJ2809——[Apio2012]dispatching

    1.题目大意:给一棵树和M值,每个点有两个权值C和L,选x个点,这x个点的C值的和不能超过M,且这x个点如果都在某个子树内 定义满意度为x*这个子树的根的L值 2.分析:这是一道可并堆的题目,我们考虑 ...

  9. NSTimer用法,暂停,继续,初始化

    NSTimer用法,暂停,继续,初始化 转载:http://blog.csdn.net/zhuzhihai1988/article/details/7742881 NSTimer的使用方法 1.初始化 ...

  10. [转载]PO BO VO DTO POJO DAO概念及其作用

    原文链接:http://jeoff.blog.51cto.com/186264/88517/ POJO = pure old java object or plain ordinary java ob ...