Virtual Directories in Glassfish
Server level
First we will look at the server level, this will make your information available server wide, outside of any application context. Lets say you have images you wish to serve in the directory C:\media. In the glassfish admin console expand configuration > http service > virtual servers. Select ‘server’ then scroll to the bottom and select ‘add property’.
For name use: alternatedocroot_1, for value use: from=/media/* dir=C:/
Keep in mind that glassfish expects the directory in the URL pattern to exist, so the from= is not just the URL pattern, but it is also the path relative to the dir=.
Luckily these changes do not require any restarts so you can experiment easily to make sure you have the settings you want. If you used the default glassfish install and your server runs at http://localhost:8080 then http://localhost:8080/media/ should now resolve to the absolute path C:\media.
The image above shows adding an alternate docroot from in the admin console, click it for the full sized image.
Application Level
Let’s say you have an application WebApplication1. By editing sun-web.xml you can add an alternate docroot that is relative to the application. For this example I will use the same as above, serving /media/* from C:/.
Add the following line to sun-web.xml inside the <sun-web-app> tag:
<property name=”alternatedocroot_1″ value=”from=/media/* dir=C:/”/>
If using the default glassfish setup, http://localhost:8080/WebApplication1/media/ should now resolve to the absolute directory C:\media
The image above shows an example of sun-web.xml. Click for full size.
In both of the above scenarios you can have as many alternate docroots as you want, as long as the follow the naming convention (alternatedocroot_1, alternatedocroot_2, etc…). Also your url pattern can be as specific as you like, for instance if you only wanted to catch requests for jpeg’s in the media directory then you would use: from=/media/*.jpg


July 28th, 2009 at 11:21
Hi, interest post. I’ll write you later about few questions!
July 29th, 2009 at 08:11
I rarely comment on blogs but yours I had to stop and say Great Blog!!
http://www.marceble.com – cool!!!!
July 30th, 2009 at 11:58
I’ve had a quick look at your site, it looks very interesting. Maybe you’d like to send a synopsis through for us to publish for you? Please make it as factual as possible and include a short bio of yourself and a link to your site at the end.
August 5th, 2009 at 21:49
i found you by link from the Directory Listing Script from Ash.. Nice to read your blog ^.^
August 6th, 2009 at 16:50
Thanks for writing, I very much liked your newest post. I think you should post more frequently, you evidently have natural ability for blogging!