0

ServletContext interface docs:

The path must begin with a / and is interpreted as relative to the current context root, or relative to the /META-INF/resources directory of a JAR file inside the web application's /WEB-INF/lib directory. This method will first search the document root of the web application for the requested resource, before searching any of the JAR files inside /WEB-INF/lib. The order in which the JAR files inside /WEB-INF/lib are searched is undefined.

I tested with Tomcat and it's not finding anything from WEB-INF/lib/*.jar files. Can the implementation differ? I have CommonRes.jar(in WEB-INF/lib) with this file inside:test.css

request.getServletContext().getResource("test.css");
request.getServletContext().getResource("/test.css");
request.getServletContext().getResource("/WEB-INF/test.css");

All this getResource() variants returns null.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Blocked
  • 340
  • 2
  • 5
  • 20
  • Your title is about interface versus implementation. How does that relate to the body of your Question? I don’t see the connection. – Basil Bourque Jan 10 '22 at 23:22
  • 1
    The text you quoted mentions that it searches within the `/META-INF/resources` directory when searching JAR files. Is your `test.css` located in that directory inside the JAR? Can you include a listing of the JAR contents in the question? (Use `jar tf WEB-INF/lib/CommonRes.jar` to get the listing.) – Tim Moore Jan 11 '22 at 00:01
  • @TimMoore, test.css is in root of CommonRes.jar. getResource() can't find in the root of a jar? – Blocked Jan 11 '22 at 11:29
  • 1
    I've just tried copying various files into the locations you mention into one of my Tomcat 10 webapps. The only place where `request.getServletContext().getResource(xxx)` is not null is for paths under the root of the war (ie context path). The search works with or without a preceeding "/". It does not find resources in any WEB-INF/lib/xyz.jar (checked both the root of the jar and as "META-INF/resources/xyz.css"). – DuncG Jan 11 '22 at 14:21
  • @DuncG so I'm right, the docs of ServletContext interface it's wrong or the implementation it's wrong. Thank you for testing. – Blocked Jan 11 '22 at 18:17
  • @BasilBourque the implementation it's not respected as in the docs of ServletContext interface. – Blocked Jan 11 '22 at 18:18
  • @Blocked That would be a “bug” or flaw, not an issue of “interface versus implementation”. – Basil Bourque Jan 11 '22 at 18:22
  • @BasilBourque , give me a good title for this question. Thank you. – Blocked Jan 11 '22 at 18:35
  • https://stackoverflow.com/a/29644548/1554397 @DuncG in the above link, it's saying that request.getServletContext().getResource(xxx) it's returning not null values only for path under the root of the war or for path like "/WEB-INF/pdf/order.pdf" (so no insde jars). For loading resources from WEB-INF/lib/*.jar, we can use classloader. It's right? – Blocked Jan 11 '22 at 18:47
  • @Blocked Yes, classloader.getResource(AsStream) is the way I normally access resources and makes code independent of servlet api. – DuncG Jan 11 '22 at 19:00

0 Answers0