Reputation: 1442
Servlet 3.0 has a great feature: I can access static resources in JARs using the META-INF/resources folder. So if I have a javascript inside my JAR I can access using http://myurl.com/myapp/myjavascript.js. That's great. But... How to to get that file from the jar to manipulate it or do something with it?
Upvotes: 0
Views: 171
Reputation: 1108772
Just use ServletContext#getResourceAsStream()
the usual way to get an InputStream
of it:
InputStream input = getServletContext().getResourceAsStream("/META-INF/javascript.js");
As to your intent to manipulate it, forget it. This resoure is not writable. Basically, you'd need to extract the whole JAR, manipulate the entry, repack the JAR and supply a custom classloader to reload it and tell the container to use it. Even if you succeed in this, all changes would get lost once you redeploy the WAR or in certain configurations even if you just restart the server, for the very simple reason that those changes are not contained in the original WAR.
You're going in the wrong path as to solving the concrete functional requirement. I recommend to take a step back and ask a new question about how to solve the particular concrete functional requirement for which you incorrectly thought that this would be the right solution.
Upvotes: 1