IllegalArgumentException在servlet 3.1应用程序中加载侦听器的属性

时间:2015-06-08 12:02:55

标签: java listener servletcontextlistener

我有ServletContextListener在尝试从文件加载属性时给我一个IllegalArgumentException。我的代码如下:

private final static Properties properties = new Properties(); 

@Override
public void contextInitialized(ServletContextEvent sce) {        
    ServletContext sc = sce.getServletContext();  

    try {                        
        // Load MyProject's properties file.
        properties.load(sc.getResourceAsStream(sc.getInitParameter("myProjectPropsFilePath")));                                        
    } 
    catch(IOException e) {                                     
        Utils.printStackTrace(e);
    }

    // Write properties to ServletContext.
    sc.setAttribute("properties", properties);
}

正在从web.xml检索上下文参数,但我不明白为什么IllegalArgumentException被抛出线:

properties.load(sc.getResourceAsStream(sc.getInitParameter("myProjectPropsFilePath")));

堆栈跟踪如下:

08-Jun-2015 12:52:07.439 SEVERE [http-apr-8080-exec-34] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class myProject.listener.SetPropertiesServletContextListener java.lang.IllegalArgumentException: The resource path [\WEB-INF\properties\MyProject.properties] is not valid at org.apache.catalina.webresources.StandardRoot.validate(StandardRoot.java:237) at org.apache.catalina.webresources.StandardRoot.getResource(StandardRoot.java:199) at org.apache.catalina.webresources.StandardRoot.getResource(StandardRoot.java:193) at org.apache.catalina.core.ApplicationContext.getResourceAsStream(ApplicationContext.java:559) at org.apache.catalina.core.ApplicationContextFacade.getResourceAsStream(ApplicationContextFacade.java:209) at myProject.listener.SetPropertiesServletContextListener.contextInitialized(SetPropertiesServletContextListener.java:26) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4738) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5158) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:702) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:697) at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:579) at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:455) at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1554) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1428) at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:885) at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:343) at javax.servlet.http.HttpServlet.service(HttpServlet.java:618) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:615) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1015) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652) at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2451) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2440) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:744)

有什么想法吗?我正在使用Apache Tomcat 8.0.3

web.xml的适当部分是:

<!-- Listeners and parameters. -->
<listener>
    <description>SetPropertiesServletContextListener</description>
    <listener-class>myProject.listener.SetPropertiesServletContextListener</listener-class>
</listener>
<context-param>
    <param-name>myProjectPropsFilePath</param-name>
    <param-value>\WEB-INF\properties\myProject.properties</param-value>
</context-param>

1 个答案:

答案 0 :(得分:1)

路径应该包含/作为分隔符,因为/ WEB-INF /在windows和linux中保持不变。您可能在web.xml中有错误的值。根据{{​​3}},你总是使用/。 这是错误的:_WaitForImageSearch('a.bmp',0,0,0,0, 30); Will wait max 30 seconds for a.bmp to appear 应该是: <param-value>\WEB-INF\properties\myProject.properties</param-value>