2

I have a JSF 2.0 application running under Glassfish 3.1.1 and I need to serve static web pages that are physically located apart from my application root that is created when my WAR file is deployed. I've found various references (like this one and this one) to defining an alternate docroot using a tag such as

<property name="alternatedocroot_1" value="from=/myimages/* dir=/images"/>

added to sun-web.xml (which I would presume means glassfish-web.xml in Glassfish 3.1.1). I can't seem to get it to work, however. In my case, I think the problem is that glassfish-web.xml simply isn't defining enough context for the alternate docroot to have any meaning:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
  <class-loader delegate="true"/>
  <jsp-config>
    <property name="keepgenerated" value="true">
      <description>Keep a copy of the generated servlet class' java code.</description>
    </property>
  </jsp-config>
  <property name="alternatedocroot_1" value="from=/myimages/* dir=/images"/>
</glassfish-web-app>

The meat of my application definition seems to be much more in web.xml than glassfish-web.xml. Do I perhaps need to put something there instead to reference the alternate docroot?

Community
  • 1
  • 1
Kevin Rahe
  • 1,609
  • 3
  • 19
  • 27

1 Answers1

5

This exact configuration requires a /images/myimages folder relative to the absolute root of the same disk as where the JVM of the webserver is running in. A file foo.png in that folder is then available by http://localhost:8080/contextname/myimages/foo.png.

A common confusion around this setting is that the from attribute is been interpreted as alone the context path in URL and not as an actual subfolder in the dir location. But this is thus not true.

If you're running Glassfish on a Windows environment then you need to specify the disk letter in the dir as well like so dir=C:/images.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Actually, under Windows it appears to search from the domain root unless you prefix the `dir=` value with `C:`. However, even when I provide a valid path, I am getting an error: `SEVERE: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException: javax.servlet.ServletException: com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class org.jboss.weld.servlet.WeldListener`. So I guess this answers my configuration question. – Kevin Rahe May 22 '12 at 20:25
  • Right. Windows is an odd beast. I've updated the answer. As to the new problem, does this exception disappear when you remove the alternate docroot? – BalusC May 22 '12 at 20:40
  • The exception did not disappear when I removed the alternate docroot, so I put it back in, restarted Glassfish and all works well now. An English description of the configuration might be helpful to some, which is: For URIs that match the `from` pattern in the alternatedocroot property's `value` attribute, the actual file path accessed is equal to the value of the `dir` path plus the portion of the URI that follows the context/app name. – Kevin Rahe May 23 '12 at 03:35
  • Oh, you just didn't restart Glassfish at all when you made changes in `glassfish-web.xml`? – BalusC May 23 '12 at 03:39
  • No. Glassfish picks up changes to `glassfish-web.xml` on the fly - even now I can change the `from` pattern, for instance, and it will update itself in a few moments. It's not uncommon for a Glassfish instance to get out of sorts, however, especially when running in my development environment (NetBeans 7.0.1). It may have even been my initial mucking around not knowing quite what I was doing that caused it to go off the rails. – Kevin Rahe May 23 '12 at 11:52
  • Is there a way to specify the dir path in a way that both linux and windows understand it? We are developing in a team some on windows/some on linux. – drame Apr 03 '14 at 14:15