web.xml文件解析,以及tomcat启动常见错误总结一哈。

[html]  view plain copy
  1. <span style="font-size:24px;">  
  2.   
  3.   
  4.   
  5.   
  6. 【1.在web.xml里配置Listener】  
  7.   
  8.   
  9. xml 代码如下:   
  10.   <listener>    
  11.         <listener-class>  
  12.         org.springframework.web.context.ContextLoaderListener   
  13.     </listener-class>     
  14.    </listener>      
  15. 如果在web.xml里给该Listener指定要加载的xml,如:  
  16.   
  17.   
  18. xml代码如下:  
  19.   
  20.   
  21. <!-- spring config -->  
  22.   
  23.   
  24.       <context-param>  
  25.   
  26.   
  27.            <param-name>contextConfigLocation</param-name>    
  28.   
  29.   
  30.            <param-value>classpath:applicationContext*.xml</param-value>  
  31.   
  32.   
  33.       </context-param>  
  34.   
  35.   
  36. 则会去加载相应的xml,而不会去加载/WEB-INF/下的applicationContext.xml。  
  37.   
  38.   
  39. 但是,如果没有指定的话,默认会去/WEB-INF/下加载applicationContext.xml。  
  40.   
  41.   
  42. 以前经常报org.springframework.web.context.ContextLoaderListener这个错误  
  43. 是因为src下的applicationContext*.xml文件没有加载到tomcat,而且所有的java文件  
  44. 也没有重新编译。所以才出现这个错误。  
  45.   
  46.   
  47. 【2.src 与  WEB-INF 的区别。】  
  48.    根据上面的例子:在<context-param>contextConfigLocation</context-param>标签中,如果不指定  
  49.     <param-value>的值,则默认是加载WEB-INF/下的applicationContext.xml文件。  
  50.     上面指定为src目录下的所有以applicationContext开头的xml文件  
  51.    下面来看一下log4jConfigLocation  
  52.    一般我们直接将log4j.properties放置在src目录下,这样系统自动会找到的,  
  53.    其实就是放在WEB-INF/classes文件下。这个路径在classpath下,所以直接就能找到。  
  54.   
  55.   
  56.    在web.xml中配置servlet,并将log4jConfigLocation加入到Servlet中,让其Server启动即运行:  
  57.     <servlet>  
  58.         <servlet-name>your servlet</servlet-name>  
  59.         <servlet-class>your servelt class</servlet-class>  
  60.         <init-param>  
  61.              <param-name>log4jConfigLocation</param-name>  
  62.              <param-value>/WEB-INF/log4j.properties</param-value>  
  63.         </init-param>  
  64.         <load-on-startup>1</load-on-startup>  
  65.     </servlet>  
  66.     这时log4j.properties文件放在WEB-INF目录下。  
  67. 【3.下面我们来深入了解一下<context-param> 与 <init-param>】  
  68.   
  69.   
  70.    其实一目了然<init-param>是tomcat服务器,创建servlet的时候才加载。  
  71.    而<context-param>是tomcat启动之后就加载。  
  72.    context-param 是ServletContext 创建的,属于整个tomcat服务器中。  
  73.   
  74.   
  75.    init-param 是有servlet创建的。  
  76.    init-param属于一个servlet所有,  
  77.    context-param属于整个应用程序所有 ,不仅是在servlet中可以得到,jsp文件中也可以得到.  
  78.    在jsp中config就相当于这里的servletContext.  
  79.   
  80.   
  81.    web.xml里面可以定义两种参数:  
  82.      (1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下:  
  83.      xml 代码  
  84.     <context-param>    
  85.            <param-name>context/param</param-name>    
  86.            <param-value>avalible during application</param-value>    
  87.     </context-param>    
  88.      (2)servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下:  
  89.     xml 代码  
  90.     <servlet>    
  91.         <servlet-name>MainServlet</servlet-name>    
  92.         <servlet-class>com.wes.controller.MainServlet</servlet-class>    
  93.         <init-param>    
  94.         <param-name>param1</param-name>    
  95.         <param-value>avalible in servlet init()</param-value>    
  96.         </init-param>    
  97.         <load-on-startup>0</load-on-startup>    
  98.     </servlet>  </span>  
[html]  view plain copy
  1. <span style="font-size:24px;"><img src="http://hi.csdn.net/attachment/201202/13/0_1329105415xtT9.gif" alt="">  
  2.     </span>  
[html]  view plain copy
  1. <span style="font-size:24px;">【web.xml】</span>  
[html]  view plain copy
  1. <pre name="code" class="html"><span style="font-size:24px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  6.       
  7.     <context-param>  
  8.   
  9.         <param-name>contextConfigLocation</param-name>   
  10.   
  11.         <param-value>classpath:conf/applicationContex*.xml</param-value>  
  12.   
  13.     </context-param>  
  14.     <context-param>    
  15.   
  16.         <param-name>log4jConfigLocation</param-name>   
  17.   
  18.         <param-value>classpath:conf/log4j.properties</param-value>  
  19.   
  20.     </context-param>  
  21.       
  22.     <listener>  
  23.         <listener-class>  
  24.             org.springframework.web.context.ContextLoaderListener  
  25.         </listener-class>  
  26.     </listener>  
  27.       
  28.     <listener>  
  29.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  30.     </listener>  
  31.           
  32.   
  33.   <servlet>  
  34.     <description>This is the description of my J2EE component</description>  
  35.     <display-name>This is the display name of my J2EE component</display-name>  
  36.     <servlet-name>MainServlet</servlet-name>  
  37.     <servlet-class>com.feihuale.test.MainServlet</servlet-class>  
  38.     <init-param>  
  39.             <param-name>contextConfigLocation_init-param</param-name>  
  40.             <param-value>WEB-INF/applicationContext-mvc.xml</param-value>  
  41.         </init-param>  
  42.         <load-on-startup>0</load-on-startup>  
  43.   </servlet>  
  44.   
  45.   <servlet-mapping>  
  46.     <servlet-name>MainServlet</servlet-name>  
  47.     <url-pattern>/*</url-pattern>  
  48.   </servlet-mapping>  
  49.     
  50.   <welcome-file-list>  
  51.         <welcome-file>index.jsp</welcome-file>  
  52.     </welcome-file-list>  
  53. </web-app></span></pre><span style="font-size:24px"><br>  
  54. <img src="http://hi.csdn.net/attachment/201202/13/0_1329105422b9ly.gif" alt=""><br>  
  55. </span>  
  56. <pre></pre>  
  57. <pre name="code" class="html"><span style="font-size:24px;">根据上面的xml文件可以通过java代码获取  
  58.     //----------------------------------------------------------  
  59.     public class MainServlet extends HttpServlet {  
  60.   
  61.   
  62.       
  63.     public void init() throws ServletException {    
  64.           
  65.           
  66.           
  67.         System.out.print("contextConfigLocation参数是存放在servletcontext中的---->");     
  68.         System.out.println(getServletContext().getInitParameter("contextConfigLocation"));    
  69.           
  70.         System.out.print("log4jConfigLoction参数是存放在servletcontext中的---->");     
  71.         System.out.println(getServletContext().getInitParameter("log4jConfigLocation"));  
  72.           
  73.         System.out.print("contextConfigLocation_init-param参数是在servlet中存放的---->");     
  74.         System.out.println(this.getInitParameter("contextConfigLocation_init-param"));    
  75.           
  76.           
  77.      }   
  78.     public void service(HttpServletRequest request, HttpServletResponse response)  
  79.             throws ServletException, IOException {  
  80.   
  81.   
  82.         response.setContentType("text/html");  
  83.         PrintWriter out = response.getWriter();  
  84.           
  85.     }  
  86.   
  87.   
  88.     }  
  89.     //----------------------------------------------------------  
  90.   
  91.   
  92. 【4.常见的tomcat启动错误。】  
  93. 在做struts+hibernate+spring项目时,启动程序后,发现错误 :  
  94.   
  95.   
  96. org.springframework.beans.factory.BeanDefinitionStoreException:   
  97. IOException parsing XML document from ServletContext resource   
  98. [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException:   
  99. Could not open ServletContext resource [/WEB-INF/applicationContext.xml]  
  100.   
  101.   
  102. 这是因为程序默认会在WEB-INF目录下查找applicationContext.xml文件,而现在程序找不到了,所以报错了。  
  103.   
  104.   
  105. 解决方法有两个:  
  106.   
  107.   
  108. 1. 把applicationContext.xml文件人工拷贝到WEB-INF目录下。  
  109.   
  110.   
  111. 2. 在配置文件中指定applicationContext.xml文件的位置:修改web.xml文件,添加行:  
  112.   
  113.   
  114.   
  115.   
  116. <context-param>  
  117.   
  118.   
  119.     <param-name>contextConfigLocation</param-name>  
  120.   
  121.   
  122.     <param-value>classpath:applicationContex*.xml</param-value>  
  123.   
  124.   
  125. </context-param >  
  126. //最后我还发现了一个问题<param-name>必须命名为contextConfigLocation,  
  127. 连大小写也不能错,不信你试试。  
  128. 推荐使用方法2。</span></pre><pre name="code" class="html"></pre><pre name="code" class="html"></pre>  
  129. <p><span style="font-family:monospace; white-space:pre"><span style="font-size:24px"><br>  
  130. </span></span></p>  
  131. <p><span style="font-size:24px"><br>  
  132. </span></p>  
  133. <p><span style="font-size:24px">参考地址:<a href="http://my.oschina.net/wxyplj/blog/14333">http://my.oschina.net/wxyplj/blog/14333</a> </span></p>  
  134. <p><span style="font-size:24px">                 <a href="http://www.fansoo.com/blog/tag/applicationcontext-xml/">http://www.fansoo.com/blog/tag/applicationcontext-xml/</a></span></p>  
  135. <p><span style="font-size:24px"><span style="white-space:pre"></span></span></p>  
  136. <br>  
  137. <pre></pre>  

文章来自于:http://blog.csdn.net/feihuale/article/details/7254396
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值