使class.getResource“ / some.properties”工作

问题描述

此JSP

<html><head><title><%=application.getServerInfo() %></title></head><body>

  <%= javax.servlet.http.HttpServlet.class.getResource("/test.properties") %>

</body></html>

在Apache Tomcat / 9.0.37上的输出

file:/projekte/apache-tomcat-9.0.37/lib/test.properties

在IBM WebSphere Liberty / 20.0.0.9上,我得到了空值。

如何加载资源?

我尝试过

test.properties in usr/servers/luna/lib/global

<library id="tr">
   <folder dir="${server.config.dir}/lib/tr" />
</library>    

<webApplication location="tr.war" type="war">
      <classloader classproviderRef="tr" />
   </webApplication>

我不会使用ServletContext.getResource(),因为我的实际问题在ear / lib类之内。

解决方法

它不是classProviderRef,而是commonLibraryRef。

<classloader commonLibraryRef="tr" />

仍然,仅当在应用程序的 中的类上调用getResource()时才能找到资源。编译的JSP com.ibm._jsp._resource

<%= getClass().getResource("/test.properties") %>

输出

file:/wlp/wlp-javaee8-20/usr/servers/luna/lib/tr/test.properties