Tuesday, October 06, 2009

Resources in web applications

A couple of components let you unpack resources in your web applications on the fly. The main idea is to pack various resources (images, css and js files) together with your code and extract data on the fly. Could be useful for example when you distribute application on the USB and your application should be up and running without installation – everything will be packed in one file.

Resource servlet lets you pickup resources (getResourceAsStream) from CLASSPATH and Unzip servlet lets you directly extract data from zip (jar) file. For example:

1. Pack your images (directory /pic) in file myfile.jar
2. Describe servlet and mapping:

Alternatively, you can define path via mapping. Set zip (jar) file as an initial parameter and define mapping for files you need to unzip:

<servlet>
<servlet-name>Unzip</servlet-name>
<servlet-class>com.jsos.unzip.UnzipServlet</servlet-class>
<init-param>
<param-name>file</param-name>
<param-value>/WEB-INF/myfile.jar</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>Unzip</servlet-name>
<url-pattern>/pic/*</url-pattern>
</servlet-mapping>

And now in your JSP files you can write so for example:

<img src="http://your_host/pic/a.gif"/>

file a.gif will be extracted on the fly

2 comments:

Veera said...

Nice approach.

Will there be any performance penalties in using this approach? Because now getting a resource also includes the unzipping time along with the network time.

Abava said...

>Will there be any performance >penalties in using this
yes, it is a classical trade: speed vs. memory
You can cache extracted data