-->

GlassFish 4.1 alternatedocroot, Access static cont

2020-03-26 06:27发布

问题:

I need to access some images with this URL http://localhost:8080/static/img1.jpg

The images are in "/home/andrea/Documents", the application context is "/". I'm using GlassFish 4.1.1.

In the /WEB-INF directory of project I've got the following: (glassfish-web.xml)

<?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>
    <parameter-encoding default-charset="UTF-8" />
    <property name="alternatedocroot_1" value="from=/static/* dir=/home/andrea/Documents" />
</glassfish-web-app>

The problem is that, the file is not found.
What did I miss?

Is there another way to define alternatedocroot` or map a virtual directory outside the project?

回答1:

I've resolved the problem reading this post: http://glassfish.10926.n7.nabble.com/unable-to-get-working-alternate-docroots-td40978.html

Unfortunately the documentation is not so clear, I try to explain how I solved my problem

The dir value is the base directory of your alternatedocroot, so you must put full path specified for the value frominto the base dir of alternatedocroot. Therefore in my case the real structure is /home/andrea/Documents/static (Remember to insert "/" at the end of dir value).

glassfish-web.xml

<?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>
    <parameter-encoding default-charset="UTF-8" />
    <property name="alternatedocroot_1" value="from=/static/* dir=/home/andrea/Documents/" />
</glassfish-web-app>

My exigence

My exigence is to have a separate directory to upload images separated from the ".war" archive. The solution I've found is insert the images in docroot directory of glassfish domain1 directory

glassfish-web.xml

<?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>
    <parameter-encoding default-charset="UTF-8" />
    <property name="alternatedocroot_1" value="from=/static/* dir=./docroot/" />
</glassfish-web-app>

Remember that the real path must contain static directory: [...]/glassfish/domains/domain1/docroot/static/



回答2:

Thanks you'are great my friend. It works for me, in my Glassfish 4.1.1.

I had this code in my glassfish-web.xml, under WEB-INF:

<?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="">
    <context-root>/GestionProcesos</context-root>
    <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>
</glassfish-web-app>

But now my glassfish-web.xml file, I have it like that:

<?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="">
    <context-root>/GestionProcesos</context-root>
    <class-loader delegate="true"/>
    <property name="alternatedocroot_1" value="from=/Downloads/* dir=/Users/Cristian/" />
</glassfish-web-app>

So, to download a file in my jsp:

<a href="${pageContext.request.contextPath}/Downloads/FileName.pdf" download="FileName.pdf">Download file</a>