Servlet for serving static content

前端 未结 14 1699
天命终不由人
天命终不由人 2020-11-22 06:02

I deploy a webapp on two different containers (Tomcat and Jetty), but their default servlets for serving the static content have a different way of handling the URL structur

相关标签:
14条回答
  • 2020-11-22 06:34

    I ended up rolling my own StaticServlet. It supports If-Modified-Since, gzip encoding and it should be able to serve static files from war-files as well. It is not very difficult code, but it is not entirely trivial either.

    The code is available: StaticServlet.java. Feel free to comment.

    Update: Khurram asks about the ServletUtils class which is referenced in StaticServlet. It is simply a class with auxiliary methods that I used for my project. The only method you need is coalesce (which is identical to the SQL function COALESCE). This is the code:

    public static <T> T coalesce(T...ts) {
        for(T t: ts)
            if(t != null)
                return t;
        return null;
    }
    
    0 讨论(0)
  • 2020-11-22 06:34

    I found great tutorial on the web about some workaround. It is simple and efficient, I used it in several projects with REST urls styles approach:

    http://www.kuligowski.pl/java/rest-style-urls-and-url-mapping-for-static-content-apache-tomcat,5

    0 讨论(0)
提交回复
热议问题