Serve files from folder outside web application in Jetty

后端 未结 2 982
后悔当初
后悔当初 2021-02-02 03:47

I have a Java web application (Eclipse/OSGI) on a Jetty server. I want to be able to serve static files to my web application from a folder outside of the web root. In my web ap

相关标签:
2条回答
  • 2021-02-02 04:21

    @Farna: In your answer I am not able to understand how you are passing the file name as VM parameter. This is what I did.

    I created testparvez.xml file in jetty webapps directory.

        <?xml version="1.0"  encoding="ISO-8859-1"?>
    <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.eclipse.org/configure.dtd">
    
    <Configure class="org.eclipse.jetty.server.handler.ContextHandler">
      <Set name="contextPath">/testparvez</Set>
      <Set name="resourceBase"><SystemProperty name="mydir"/></Set>
      <Set name="handler">
        <New class="org.eclipse.jetty.server.handler.ResourceHandler">
          <Set name="welcomeFiles">
            <Array type="String">
              <Item><SystemProperty name="myfile"/></Item>
            </Array>
          </Set>
          <Set name="cacheControl">max-age=3600,public</Set>
        </New>
      </Set>
    </Configure>
    

    Then I start jetty as

    java -jar start.jar jetty.port=8082 -Dmydir=C:/test/javadoc/ -Dmyfile=index.html
    

    And finally I access from url http://localhost:8082/testparvez/

    0 讨论(0)
  • 2021-02-02 04:25

    Solved it!

    This is what I added to my jetty.xml file:

    <Set name="handler">
        <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
            <Set name="handlers">
                <Array type="org.eclipse.jetty.server.Handler">
                    <Item>
                        <New class="org.eclipse.jetty.server.handler.ContextHandler">
                            <Set name="contextPath">/myContextPath</Set>
                            <Set name="handler">
                                <New class="org.eclipse.jetty.server.handler.ResourceHandler">
                                    <Set name="directoriesListed">false</Set>
                                    <Set name="resourceBase">/actual/folder/on/file/system</Set>
                                </New>
                            </Set>
                        </New>
                    </Item>
                    [...other handlers...]
                </Array>
            </Set>
        </New>
    </Set>
    
    0 讨论(0)
提交回复
热议问题