码迷,mamicode.com
首页 > Web开发 > 详细

web.xml

时间:2020-03-10 16:20:39      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:anon   Fix   对象   ase   gb2312   max   lan   try   container   

一、web 应用相关

<!-- Web 应用图标:指出 IDE 和 GUI 工具用来表示 Web 应用的小图标和大图标 -->
<icon>
    <small-icon>/images/app_small.gif</small-icon>
    <large-icon>/images/app_large.gif</large-icon>
</icon>

<!-- Web 应用名称:提供 GUI 工具可能会用来标记这个 WEB 应用的名称 -->
<display-name>MyWebApplication</display-name>

<!-- Web 应用描述:声明 WEB 应用的说明性文本 -->
<description></description>

二、上下文参数

声明应用范围内的初始化参数。

<context-param>
    <!-- 日志配置路径 -->
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:config/log4j.properties</param-value>
</context-param>
    
<context-param>
    <!-- 日志页面刷新间隔 -->
    <param-name>log4jRefreshInterval</param-name>
    <param-value>6000</param-value>
</context-param>
    
<context-param>
    <!-- Spring 配置文件路径。默认的路径是 /WEB-INF/applicationContext.xml -->
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/spring-context.xml</param-value>
</context-param>

<context-param>
    <!-- 使用匹配符 -->
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>

三、过滤器配置

将一个名字与一个实现 javaxs.servlet.Filter 接口的类相关联。

<filter>
    <filter-name>setCharacterEncoding</filter-name>
    <filter-class>com.test.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>

四、监听器配置

指出事件监听程序类。

事件监听程序在建立、修改和删除会话或 servlet 环境时收到通知。

<listener>
    <!-- ContextLoaderListener 的作用是启动 Web 容器时,自动装配ApplicationContext.xml 的配置信息。
    https://www.cnblogs.com/muyuchengguang/p/9570842.html -->
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
    <!-- 自定义实现了 ServletContextListener 接口的监听类。
       ServletContextListener 接口能够监听 ServletContext 对象的生命周期,
       因为每个 web 应用仅有一个 ServletContext 对象,所以实际上该接口监听的是整个 web 应用。 -->
    <listener-class>com.listener.MyServletContextListener</listener-class>
</listener>

五、Servlet 配置

服务器一般为 servlet 提供一个缺省的 URL:http://host/webAppPrefix/servlet/ServletName。但通常会更改这个 URL 以便 servlet 可以访问初始化参数或更容易处理相对 URL。

在更改缺省 URL 时,使用 servlet-mapping 元素。

<!-- 声明一个 servlet 的数据 -->
<servlet>
    <!-- 指定 servlet 的名称 -->
    <servlet-name>snoop</servlet-name>
    <!-- 执行 servlet 的类名称 -->
    <servlet-class>SnoopServlet</servlet-class>
    <!-- 定义参数,可以有多个 init-param。
        在 servlet 类中通过 getInitParameter(String name) 方法访问初始化参数 -->
    <init-param>
        <param-name>foo</param-name>
        <param-value>bar</param-value>
    </init-param>
    <!-- 指定当 Web 应用启动时,装载 Servlet 的次序。
        1. 值为整数或 0 时,Servlet 容器先加载数值小的 servlet,再依次加载其他数值大的 servelt;
        2. 值为负数或未定义时,Servlet 容器将在 Web 客户首次访问这个 servlet 时加载它。 -->
    <load-on-startup>1</load-on-startup>
    <run-as>
        <description>Security role for anonymous access</description>
        <role-name>tomcat</role-name>
    </run-as>
</servlet>

<!-- 用来定义 servlet 所对应的 URL -->
<servlet-mapping>
    <!-- 指定 servlet 的名称 -->
    <servlet-name>snoop</servlet-name>
    <!-- 指定 servlet 所对应的 URL -->
    <url-pattern>/snoop</url-pattern>
</servlet-mapping>

六、会话超时配置

如果某个会话在一定时间内未被访问,服务器可以抛弃它以节省内存。

可通过使用 HttpSession 的 setMaxInactiveInterval 方法明确设置单个会话对象的超时值,或者可利用 session-config 元素指定缺省超时值。

单位为分钟。

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

七、MIME 类型配置

如果 Web 应用具有想到特殊的文件,希望能保证给他们分配特定的 MIME 类型,则 mime-mapping 元素提供这种保证。

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

八、指定欢迎文件页配置

指示服务器在收到引用一个目录名而不是文件名的 URL 时,使用哪个文件。

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

九、配置错误页面

  1. 通过错误码来配置

    <error-page>
        <error-code>404</error-code>
    <location>/NotFound.jsp</location>
    </error-page>

    当系统发生 404 错误时,跳转到错误处理页面 NotFound.jsp。

  2. 通过异常的类型来配置

    <error-page>
        <exception-type>java.lang.NullPointerException</exception-type>
    <location>/error.jsp</location>
    </error-page>

    当系统发生 java.lang.NullPointerException(空指针异常)时,跳转到错误处理页面 error.jsp。

十、TLD 配置

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

十一、资源管理对象配置

<!-- 声明与资源相关的一个管理对象 -->
<resource-env-ref>
    <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
</resource-env-ref>

十二、资源工厂配置

<!-- 声明一个资源工厂使用的外部资源 -->
<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/test_db</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

十三、安全限制配置

<!-- 指定应该保护的 URL -->
<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>

十四、登录验证配置

<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>

十五、安全角色

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

<security-role>
    <role-name>romcat</role-name>
</security-role>

十六、Web 环境参数

<!-- env-entry 元素声明 Web 应用的环境项 -->
<env-entry>
    <env-entry-name>mainExemptions</env-entry-name>
    <env-entry-value>1</env-entry-value>
    <env-entry-type>java.lang.Integer</env-entry-type>
</env-entry>

十七、EJB 声明

<!-- 声明一个 EJB 的主目录的引用 -->
<ejb-ref>
    <description>Example EJB reference</description>
    <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>

<!-- 声明一个 EJB 的本地主目录的引用 -->
<ejb-local-ref>
    <description>Example Local EJB reference</description>
    <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>

十八、配置 DWR

<servlet>
    <servlet-name>dwr-invoker</servlet-name>   
    <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

十九、配置 Struts

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts2.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>detail</param-name>
        <param-value>2</param-value>
    </init-param>
    <init-param>
        <param-name>debug</param-name>
        <param-value>2</param-value>
    </init-param>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
        <param-name>application</param-name>
        <param-value>ApplicationResources</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
    
<jsp-config>
    <taglib>
        <taglib-uri>struts-bean</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>struts-html</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>struts-nested</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>struts-tiles</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
    </taglib>
</jsp-config>

二十、配置 Spring

基本都是在 Struts 中配置。

<!-- 指定 spring 配置文件位置 -->
<context-param>
    <!-- Spring 会使用这个参数去加载所有逗号分隔的 xml 文件。
    如果没有这个参数,Spring默认加载 WEB-INF/applicationContext.xml文件 
    
    原理:https://www.cnblogs.com/javahr/p/8408857.html 或查看 ContextLoaderListener 类源码 
    configLocationParam = sc.getInitParameter("contextConfigLocation")
    -->
    <param-name>contextConfigLocation</param-name>
    <param-value>
        <!-- 加载多个 spring 配置文件 -->
        classpath:config/spring-context.xml, 
        classpath:config/spring-mvc.xml
    </param-value>
</context-param>
    
<!-- 定义 Spring 监听器,加载 spring -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
    
<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

Web.xml配置文件

web.xml

标签:anon   Fix   对象   ase   gb2312   max   lan   try   container   

原文地址:https://www.cnblogs.com/dins/p/webxml.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!