您的位置:首页 > 其它

web.xml文件的作用及基本配置

2016-06-28 19:02 603 查看
web.xml文件并不是web工程必须的。

web.xml文件是用来配置:欢迎页、错误页、session过期时间、servlet、filter、listener等的。

当你的web工程没用到这些时,你可以不用web.xml文件来配置你的web工程。

 

1、指定欢迎页面,例如:

<welcome-file-list>

  <welcome-file-list>

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

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

  </welcome-file-list>

上面的例子指定了2个欢迎页面,显示时按顺序从第一个找起,如果第一个存在,就显示第一个,后面的不起作用。如果第一个不存在,就找第二个,以此类推。

2、命名与定制URL。我们可以为Servlet和JSP文件命名并定制URL,其中定制URL是依赖一命名的,命名必须在定制URL前。下面拿serlet来举例:

(1)、为Servlet命名:

<servlet>

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

    <servlet-class>net.test.TestServlet</servlet-class>

</servlet>

(2)、为Servlet定制URL、

<servlet-mapping>

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

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

</servlet-mapping>

3、定制初始化参数:可以定制servlet、JSP、Context的初始化参数,然后可以在servlet、JSP、Context中获取这些参数值。下面拿servlet来举例:

<servlet>

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

    <servlet-class>net.test.TestServlet</servlet-class>

    <init-param>

          <param-name>userName</param-name>

          <param-value>Tommy</param-value>

    </init-param>

    <init-param>

          <param-name>E-mail</param-name>

          <param-value>Tommy@163.com</param-value>

    </init-param>

</servlet>

经过上面的配置,在servlet中能够调用getServletConfig().getInitParameter("param1")获得参数名对应的值。

4、设置会话(Session)过期时间,其中时间以分钟为单位,假如设置60分钟超时:
<session-config>
<session-timeout>60</session-timeout>
</session-config>

5、配置过滤器类及过滤的url路径

<filter> 

   <filter-name>GlobalFilter</filter-name> 

          <filter-class>com.rd.web.filter.GlobalFilter</filter-class> 

</filter>

 <filter-mapping> 

  <filter-name>GlobalFilter</filter-name> 

         <url-pattern>/*</url-pattern> 

​ </filter-mapping>

6、设置监听器

<listener>

    <listener-class>

        com.rd.web.listener.WebConfigContextListener

    </listener-class>

  </listener>

7、配置Spring容器

(1)配置spring配置文件的位置,当有多个配置文件时用逗号(,)分隔,路径可用通配符(*),如果不设置contextConfigLocation的初始参数则默认会读取WEB-INF路径下的
application.xml文件

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:/spring/application*.xml,classpath:/spring/dev/application*.xml</param-value> 
</context-param>

(2)​配置spring监听,ContextLoaderListener实现ServletContextListener,用于读取contextConfigLocation中定义的spring的xml文件,ContextLoaderListener读取这些XML文件并产生
WebApplicationContext对象,然后将这个对象放置在ServletContext的属性里,这样我们只要可以得到Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring容器管理的bean

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

可见servlet是web应用开发的底层的东西,及时是spring的启动,也是通过继承这些底层的servlet类

8、配置struts

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

  </filter-mapping>

9、配置Url重写过滤器

<filter>

    <filter-name>UrlRewriteFilter</filter-name>

    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>

    <init-param>

           <param-name>logLevel</param-name>

           <param-value>WARN</param-value>

    </init-param>

</filter>

<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>

</filter-mapping>

10、配置springMVC

<servlet>

    <servlet-name>springMvc</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

      <param-name>contextConfigLocation</param-name>

      <param-value>classpath:/spring/ApplicationContext-mvc.xml</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>springMvc</servlet-name>

    <url-pattern>/</url-pattern>

  </servlet-mapping>

11、配置字符过滤器及其作用

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

12、配置错误页面
   

(1) 通过错误码来配置error-page    
   <error-page>    
      <error-code>404</error-code>    
      <location>/NotFound.jsp</location>    
   </error-page>    
上面配置了当系统发生404错误时,跳转到错误处理页面NotFound.jsp。    
(2)通过异常的类型配置error-page    
   <error-page>    
       <exception-type>java.lang.NullException</exception-type>    
       <location>/error.jsp</location>    
   </error-page>    
上面配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  web.xml 作用 配置