views:

1047

answers:

5

Is it possible to store web content (such as JSPs, HTML, images, CSS etc) in a JAR file?

I've been looking at various options at modularising our web applications and this is one possibility.

We are currently using JSF and Facelets for our view technology - I'm thinking it may be possible to write some form of custom view resolver which would examine the classpath rather than a filesystem directory, but I'm not sure this would work.

Any ideas would be appreciated! :)

Update: I should probably clarify. How do you get the web container (such as Tomcat) to load resources from a JAR file? For example, I deploy a .war file with my web application. If I access /index.jsp, the container will try to look in the web content directory for a file named index.jsp.

Is there an easy way to configure your own resource loader using Tomcat or the like so that it searches the classpath as well as the filesystem?

+1  A: 

Absolutely. Heck, you can store content directly in a WAR file, which is basically a JAR file with a few extra bits. Yes, you may need to write a custom resolver to use ClassLoader.getResourceAsStream, but basically as you're given the ability to generate the content however you like, fetching it from a jar file seems perfectly reasonable. You'll probably want to make sure it only fetches a very specific set of extensions though :)

Jon Skeet
+1  A: 

Yes, it is possible to store files in a JAR file, and pull them at runtime.

To load a resource such as property file, xml, xslt, image etc; from your deployment jar using the following.

this.getClass().getClassLoader().getResourceAsStream( filename ) ;
Martin Spamer
+2  A: 

You can also use the weblets project (see https://weblets.dev.java.net/).

You store some resources in a JAR library (such as images, css, javascript...) and you write a really simple weblet-config.xml. Then in the JSF page, you can refer them directly with this syntax:

<h:graphicImage src="weblet://some-name/images/someimage.jpg" .../>
romaintaz
Thanks, this is really useful. Now if only there was a way of getting JSF pagse themselves into a .jar file...
Phill Sacre
+1  A: 

A tag file is like a JSP fragment that can be placed in a jar. Using tag files, could help you, but I have never tried to use images, CSS, etc. in a jar.

Dave