29

Some time ago I read an article what is expected to be in Servlet API 3.0. I remember I read that you can save some .jsp files in a /WEB-INF/lib/somelib.jar/META-INF/web/.jsp and this resources will be exposed to the context root of the web application.

I have just installed Tomcat7 to give it a try, but I found no documentation that I can put some web-resources in jar-files.

  • Does this feature exists in servlet api 3.0?
  • Does this feature exists in tomcat 7.0?
  • Is there another way to have jsp-files in /lib/*.jar files?
Igor Mukhin
  • 15,014
  • 18
  • 52
  • 61

1 Answers1

46

There is a feature in servlet 3.0 that allows you to package resources (images, jsp, etc.) in a JAR file. What you do is in your jar file, you create META-INF/resources and dump anything you want in there including directories for structuring your resources. What happens is that META-INF/resources will be mapped to the docroot of your web application.

When there is a clash of resource between your app and the JAR file, your apps resource will be returned. See this

Tomcat 7 supports Servlet 3 so it should support this feature

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Chuk Lee
  • 3,570
  • 22
  • 19
  • 2
    Thanks. I tested this with Tomcat 7.0.6 and it works. Important that this works only with jars in WEB-INF/lib, not in WEB-INF/classes – Igor Mukhin Jan 19 '11 at 08:48
  • Yes only with JAR files in lib. This is actaully for supporting frameworks where the framework can serve their own resources – Chuk Lee Jan 19 '11 at 17:12
  • I'm using a spring-js-resources.jar file, and all files are located under META-INF/web-resources, not by META-INF/resources, also I start embeddable tomcat 7 using maven plugin and it does not work. is there any solution? – Alexandr Apr 23 '13 at 07:53
  • @Alexandr I don't think you can remap resources to web-resources – Chuk Lee Apr 24 '13 at 06:04
  • 1
    @ChukLee, i've found that it's possible http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-static-resources, but for some reason it does not work in my case.cannot understand why:( – Alexandr Apr 24 '13 at 06:08
  • according to my web.xml I'm using Servlet 2.5. I placed the resources in my jar under META-INF/resources. When I ran my app locally (with Jetty) it worked fine, but after deploying it to a Tomcat server, then it didn't work. What might be wrong? – CCC Mar 11 '14 at 17:00
  • @CarlosP This is only supported in Servlet 3. Also you have to place your JAR in WEB-INF/lib in your WAR for this to work. – Chuk Lee Mar 12 '14 at 00:42
  • I have a library with a JS file placed in it according to the instructions, and two war projects using the library as JAR (both have servlet API 3.0.1). In one of the projects the JS file is accessible in the container, in another it isn't. Checked web.xml of both projects and don't see what's wrong :( – vadipp Feb 10 '22 at 09:17