Web.xml 常用元素(网上资源整理)
<web-app>
1.上下文初始化参数
在sevlet的方法中可以通过如下方式获取context-param的参数
在jsp中 直接使用页面隐含对象application或者上面的方式;
2. 过滤器
作用:将一个名字与一个实现javax.servlet.Filter 接口的类相关联,一旦命名了一个过滤器,就要利用filter-mapping元素把它与一个或多个servlet或JSP页面相关联。
3. 指示服务器在收到【应用服务名时目录名】访问的文件,以welcome-file的顺序依次寻找,下面配置将会先打开index.htm文件,找不到时,才会打开index.jsp文件;
4. 在返回特定HTTP 状态代码 时,或者特定类型的异常被抛出时,能够制定将要显示的页面。
5.监听 servlet API 的版本2.3+才支持监听,让建立、修改和删除会话request,response ,session等 环 境时得到通知。
listener-class 为实现了Listener接口的类
6.回话超时设置 可通过使用HttpSession的setMaxInactiveInterval方法明确设置单个会话对象的超时值,会话在一定时间内未被访问,服务器可以抛弃它以节省内存,或者可利用session-config元素制定缺省超时值。
7、如果Web应用具有想到特殊的文件 ,希望能保证给他们分配特定的MIME类型,则mime-mapping元素提供这种保证。
8.对标记库 描述符文件指定别名。此功能使你能够更改TLD文件的位置,而不用编辑使用这些文件的JSP页面
9.资源管理对象 配置
10.资源工厂配置
11.高级servlet配置
写道
12.Web应用图标:指出IDE和GUI工具用来表示Web应用的大图标和小图标
13.安全限制配置
14.登陆验证配置
15.配置DWR
16.配置Struts
17.spring 配置
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配置文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>arch4</display-name> <!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring-Context loader --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/*.xml</param-value> </context-param> <!--Spring ApplicationContext 载入 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <display-name>InitEbsConfig</display-name> <servlet-name>InitEbsConfig</servlet-name> <servlet-class>com.suptools.util.InitEbsConfig</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 著名 Character Encoding filter --> <filter> <filter-name>encodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <!--Hibernate Open Session in View Filter,在每次的整个request过程中,对数据库的多次操作使用同一个Hibernate Session hibernateFilter位置放在此处是为解决延迟加载和事务提交的问题 <filter> <filter-name>hibernateFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <!-- 说明1:在Servlet2.4版本的web.xml中添加了<dispatcher>标签,这个元素可能有4种值,即REQUEST、FORWARD、INCLUDE、ERROR,可以在一个<filter-mapping>元素中添加任意数目的<dispatcher>,使得filter将会作用于直接从客户端发过来的request,通过forward过来的request,通过include过来的request,和通过<error-page>过来的request,如果没指定任何<dispatcher>则默认为REQUEST 说明2:jsp本身就是一个扩展后的servlet,所以它所具有的标签都是采用servlet机制的,struts1支持,因为它本身就是采用servlet的api开发的。但struts2不支持,因为它是采用filter开发的,所以在jsp中使用forward方式的请求跳转不到Action类。需要添加<dispatcher>标签才行。 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> <!-- Spring 刷新Introspector防止内存泄露 --> <listener> <listener-class> org.springframework.web.util.IntrospectorCleanupListener </listener-class> </listener> <!-- session超时定义,单位为分钟,清除服务端我们存储在Session中的对象,不清除Tomcat容器存储在Session中的对象 --> <session-config> <session-timeout>30</session-timeout> </session-config> <!-- 自动对账监听器 --> <listener> <listener-class>com.suptools.timer.TimerListener</listener-class> </listener> <!-- 默认首页定义 --> <welcome-file-list> <welcome-file>logon.jsp</welcome-file> </welcome-file-list> </web-app>
更多文章见:http://www.16boke.com