web.xml文件解析,以及tomcat启动常见错误总结一哈。
2012-02-13 11:55
756 查看
【1.在web.xml里配置Listener】 xml 代码如下: <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> 如果在web.xml里给该Listener指定要加载的xml,如: xml代码如下: <!-- spring config --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> 则会去加载相应的xml,而不会去加载/WEB-INF/下的applicationContext.xml。 但是,如果没有指定的话,默认会去/WEB-INF/下加载applicationContext.xml。 以前经常报org.springframework.web.context.ContextLoaderListener这个错误 是因为src下的applicationContext*.xml文件没有加载到tomcat,而且所有的java文件 也没有重新编译。所以才出现这个错误。 【2.src 与 WEB-INF 的区别。】 根据上面的例子:在<context-param>contextConfigLocation</context-param>标签中,如果不指定 <param-value>的值,则默认是加载WEB-INF/下的applicationContext.xml文件。 上面指定为src目录下的所有以applicationContext开头的xml文件 下面来看一下log4jConfigLocation 一般我们直接将log4j.properties放置在src目录下,这样系统自动会找到的, 其实就是放在WEB-INF/classes文件下。这个路径在classpath下,所以直接就能找到。 在web.xml中配置servlet,并将log4jConfigLocation加入到Servlet中,让其Server启动即运行: <servlet> <servlet-name>your servlet</servlet-name> <servlet-class>your servelt class</servlet-class> <init-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/log4j.properties</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> 这时log4j.properties文件放在WEB-INF目录下。 【3.下面我们来深入了解一下<context-param> 与 <init-param>】 其实一目了然<init-param>是tomcat服务器,创建servlet的时候才加载。 而<context-param>是tomcat启动之后就加载。 context-param 是ServletContext 创建的,属于整个tomcat服务器中。 init-param 是有servlet创建的。 init-param属于一个servlet所有, context-param属于整个应用程序所有 ,不仅是在servlet中可以得到,jsp文件中也可以得到. 在jsp中config就相当于这里的servletContext. web.xml里面可以定义两种参数: (1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下: xml 代码 <context-param> <param-name>context/param</param-name> <param-value>avalible during application</param-value> </context-param> (2)servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下: xml 代码 <servlet> <servlet-name>MainServlet</servlet-name> <servlet-class>com.wes.controller.MainServlet</servlet-class> <init-param> <param-name>param1</param-name> <param-value>avalible in servlet init()</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet>
【web.xml】
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/applicationContex*.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:conf/log4j.properties</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>MainServlet</servlet-name> <servlet-class>com.feihuale.test.MainServlet</servlet-class> <init-param> <param-name>contextConfigLocation_init-param</param-name> <param-value>WEB-INF/applicationContext-mvc.xml</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MainServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
根据上面的xml文件可以通过java代码获取 //---------------------------------------------------------- public class MainServlet extends HttpServlet { public void init() throws ServletException { System.out.print("contextConfigLocation参数是存放在servletcontext中的---->"); System.out.println(getServletContext().getInitParameter("contextConfigLocation")); System.out.print("log4jConfigLoction参数是存放在servletcontext中的---->"); System.out.println(getServletContext().getInitParameter("log4jConfigLocation")); System.out.print("contextConfigLocation_init-param参数是在servlet中存放的---->"); System.out.println(this.getInitParameter("contextConfigLocation_init-param")); } public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); } } //---------------------------------------------------------- 【4.常见的tomcat启动错误。】 在做struts+hibernate+spring项目时,启动程序后,发现错误 : org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 这是因为程序默认会在WEB-INF目录下查找applicationContext.xml文件,而现在程序找不到了,所以报错了。 解决方法有两个: 1. 把applicationContext.xml文件人工拷贝到WEB-INF目录下。 2. 在配置文件中指定applicationContext.xml文件的位置:修改web.xml文件,添加行: <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContex*.xml</param-value> </context-param > //最后我还发现了一个问题<param-name>必须命名为contextConfigLocation, 连大小写也不能错,不信你试试。 推荐使用方法2。
参考地址:http://my.oschina.net/wxyplj/blog/14333
http://www.fansoo.com/blog/tag/applicationcontext-xml/
相关文章推荐
- web.xml文件解析,以及tomcat启动常见错误总结一哈。
- java web程序中web.xml文件中servlet-name、url-pattern的值不能与映射的类名一致,否则tomcat启动不了,报错误: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Servlet_day02]]
- 启动tomcat服务时,报解析/WEB-INF/applicationContext.xml异常或找不到该文件
- Tomcat8.x 启动start()->上下文发布->上下文中web.xml文件解析
- 01、配置tomcat的web.xml文件,使得方法在tomcat启动时被加载
- Tomcat启动解析web.xml源码分析
- Tomcat启动过程原理详解 -- 非常的报错:涉及了2个web.xml等文件的加载流程
- IDEA配置项目及启动(web.xml引mybatis配置文件总是出现错误)
- 解析xml文件常见错误
- Tomcat的配置文件server.xml中添加中文注释后,启动Tomcat时出现错误
- WEB.XML设置错误,导致TOMCAT不能正常启动
- 监听总结之解析问题二:由sqlnet.ora文件寻到EZCONNECT适配器程序来解析主机名为主机上的多个ip地址(*) 以及 tnsping是用来测试某个配置的监听程序是否启动
- tomcat配置文件web.xml与server.xml解析--重要
- tomcat conf目录下 web.xml,tomcat-users.xml,server.xml以及context.xml四个文件的作用
- 关于jsp网页以及web.xml文件配置总结
- tomcat配置文件web.xml与server.xml解析--重要
- tomcat配置文件web.xml与server.xml解析--重要
- 项目启动时报 web.xml; lineNumber: 1; columnNumber: 1; 文件提前结束 错误
- tomcat源码解析(二)--web.xml文件的解析
- maven项目:spring配置里:连接远程oracle(虚拟机里),【虚拟机没开启(数据库连接不上)导致tomcat启动失败(web.xml配置Spring了监听器)】=(也不报具体错误,就是超时)