web.xml 各个标签详解

对于web项目,web.xml可有,也可以没有。但是对于大型的web项目使用web.xml会很方便的。

1.web.xml是项目的入口

2.web.xml可以做的事:

  • 指定欢迎页面
  • 指定错误页面、
  • 命名与定制url
  • 定制初始化参数
  • 设置过滤器
  • .设置监听器
  • 设置session过期时间

 

1<?xml version="1.0" encoding="UTF-8"?> 

     声明xml的版本

2<web-app> 

    指明xml使用的是哪个模式文件(即xsd,就是描述xml的规范)

3<display-name>

    设置站点的标题显示内容

4<session-config>

    设置用户登录的session时间 

5<welcome-file-list>

    设置访问站点项目名显示的欢迎页面,第一个不存在就显示第二个。第一个存在第二个就会不起作用。

6<servlet>

   指定访问的所有路径都经过TestServlet.do处理器。

7<context-param /> 

用来设定web站台的环境参数,参数也可以xml文件。即加载各种配置文件。

8  <listener>

设置监听器

9<filter>

设置过滤器。

10<error-page>

指定错误的页面,可以通过“异常类型”或“错误码”指定错误处理页面

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>CLNContract</display-name>
    <!-- session周期30分钟失效 -->
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <!-- session周期30分钟失效 -->

<welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>index1.jsp</welcome-file>

</welcome-file-list>

<servlet>

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

<error-page>

    <error-code>404</error-code>

    <location>/error404.jsp</location>

</error-page>

    <servlet-name>servlet1</servlet-name>

    <servlet-class>com.TestServlet</servlet-class>

</servlet>

<servlet-mapping>

    <servlet-name>servlet1</servlet-name>

    <url-pattern>*.do</url-pattern>

</servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> 
        classpath*:applicationContext*.xml
        </param-value>
    </context-param>
  <filter>
    <filter-name>systemFilter</filter-name>
    <filter-class>com.chinalife.util.UrlFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>systemFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
       <filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ActionContextCleanUp
        </filter-class>
    </filter>
<!--
    <filter>
        <filter-name>loginAndStrutsFilter</filter-name>
        <filter-class>com.chinalife.common.web.filter.LoginAndStrutsFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>loginAndStrutsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  -->
    <!-- <filter> <filter-name>loginAndStrutsFilter</filter-name>
     <filter-class>com.chinalife.common.web.filter.LoginAndStrutsFilter</filter-class> 
        </filter> <filter-mapping> 
        <filter-name>loginAndStrutsFilter</filter-name> 
        <url-pattern>/*</url-pattern> </filter-mapping> -->
    <!-- struts 2.3.15.1 Config begin -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    <!-- Spring 3.2.4.RELEASE -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
    <listener-class>com.chinalife.common.web.filter.SpringInit</listener-class>
    </listener>
    <listener>
        <listener-class>com.chinalife.contract.userPermissionQuery.utils.NFDFlightDataTaskListener</listener-class>
    </listener>
    <filter>
        <filter-name>OpenSessionInView</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>flushMode</param-name>
            <param-value>AUTO</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInView</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping><!--

    <filter>
        <filter-name>urlFilter</filter-name>
        <filter-class>com.chinalife.common.web.filter.UrlFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>urlFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    --><servlet>
        <servlet-name>XFireServlet</servlet-name>
        <servlet-class>
            org.codehaus.xfire.spring.XFireSpringServlet
        </servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>services.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>XFireServlet</servlet-name>
        <url-pattern>/service/*</url-pattern>
    </servlet-mapping>

    <jsp-config>
        <taglib>
            <taglib-uri>/WEB-INF/ecside.tld</taglib-uri>
            <taglib-location>/WEB-INF/ecside.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/user-defined-tags</taglib-uri>
            <taglib-location>/common/tlds/user-defined-tags.tld</taglib-location>
        </taglib>
    </jsp-config>

    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
    
   <mime-mapping>
    <extension>xls</extension>
    <mime-type>application/msexcel</mime-type>
  </mime-mapping> 
  <mime-mapping>
    <extension>txt</extension>
    <mime-type>applcation/txt</mime-type>
  </mime-mapping> 
</web-app>
 

猜你喜欢

转载自blog.csdn.net/fxbfxb111/article/details/84786611