Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving static content from a jarfile with Jetty?

Tags:

java

jruby

jetty

This should be fairly easy, but for some reason nearly everything I try just seems to hand out a 'not found' error when I hook it up to a web browser.

I've got a single static context, and for the ResourceBase I've got 'file:jar:/path/to/myjar!/.'... any ideas what I'm missing?

like image 832
Don Werve Avatar asked Aug 08 '09 18:08

Don Werve


People also ask

How is static content served?

There are three steps to requesting static content from a server: A user sends a request for a file to the web server. The web server retrieves the file from disk. The web server sends the file to the user.

What is context path in Jetty?

The context path is the prefix of a URL path that is used to select the web application to which an incoming request is routed. Typically a URL in a Java servlet server is of the format http://hostname.com/contextPath/servletPath/pathInfo, where each of the path elements may be zero or more / separated elements.

What does eclipse jetty do?

The Eclipse Jetty Project Jetty provides a web server and servlet container, additionally providing support for HTTP/2, WebSocket, OSGi, JMX, JNDI, JAAS and many other integrations. These components are open source and are freely available for commercial use and distribution.

Does jetty use Apache?

In terms of licensing, Tomcat enjoys the Apache 2.0 open source license, while Jetty is dual licensed through both the Apache 2.0 License and the Eclipse Public License 1.0.


1 Answers

Try to load the resource from classloader like this,

    ClassLoader classLoader =
                Thread.currentThread().getContextClassLoader();

        if (classLoader == null) {
            classLoader = getClass().getClassLoader();
        }


        InputStream stream = classLoader.getResourceAsStream(name);

Your approach assumes absolute path and it may not be true when the server is deployed. The jar could be in another JAR (WAR) or a temporary directory.

like image 111
ZZ Coder Avatar answered Oct 16 '22 06:10

ZZ Coder