Search code examples
glassfish-3

Glassfish 3 - Loading images from a static server


I'm trying to load images (and other static content) from a server outside of my web application which is deployed to Glassfish v3. I have the following configs in the web.xml but it does not work on Glassfish (but it works on Tomcat):

<servlet>
    <servlet-name>ExternalImagesServlet</servlet-name>
    <servlet-class>com.example.servlet.HttpProxyServlet</servlet-class>
    <init-param>
        <param-name>RemoteURI</param-name>
        <param-value>http://ip.of.second.server/website-files</param-value>
    </init-param>   
    <init-param>
        <param-name>AllowedContentTypes</param-name>
        <param-value>image/gif,image/jpeg,image/png</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>ExternalImagesServlet</servlet-name>
    <url-pattern>/images/*</url-pattern>
</servlet-mapping>

Where ip.of.second.server is an actual IP address of the server. I have the file called website-files.xml defined as follow:

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="d:/internet/website/images" />

And website-files.xml is saved to glassfish\domains\domain1\config directory. But Glassfish does not pick up this config file.

I have looked at Oracle Glassfish configuration doco but there's no mention on how you can reference images from a different server.

Please help.


Solution

  • I have solved it based on an old thread relating to Glassfish version 2 that I found on Google after two days of search.

    In case anyone is interested in the solution, here it is:

    1) Create a file called sun-web.xml directly under Webcontent\WEB-INF directory and add the following configuration to this file:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD GlassFish Application Server 3.0 Servlet 3.0//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_3_0-0.dtd">
    <sun-web-app>
       <property name="alternatedocroot_1" value="from=/images/* dir=d:/internet/website" />
       <property name="alternatedocroot_2" value="from=/files/* dir=d:/internet/website" />
    </sun-web-app>
    

    2) Remove the servlet and servlet-mapping configurations from web.xml file (like I did above). Note: The above would work if you were to use Tomcat.

    3) Delete the website-files.xml from glassfish\domains\domain1\config directory as this file is not needed by Glassfish: Note: This file is needed by Tomcat.