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

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

  1. <filter>  
  2.       <filter-name>Struts2</filter-name>  
  3.       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  4.   </filter>  
  5.   <filter-mapping>  
  6.       <filter-name>Struts2</filter-name>  
  7.       <url-pattern>/*</url-pattern>  
  8.   </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、配置字符过滤器及其作用

  1. <filter>  
  2.         <filter-name>encodingFilter</filter-name>  
  3.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  4.         <init-param>  
  5.             <param-name>encoding</param-name>  
  6.             <param-value>UTF-8</param-value>  
  7.         </init-param>  
  8.         <init-param>  
  9.             <param-name>forceEncoding</param-name>  
  10.             <param-value>true</param-value>  
  11.         </init-param>  
  12.     </filter>  
  13.     <filter-mapping>  
  14.         <filter-name>encodingFilter</filter-name>  
  15.         <url-pattern>/*</url-pattern>  
  16.     </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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值