Servlet for serving static content

前端 未结 14 1941
天命终不由人
天命终不由人 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:27

    There is no need for completely custom implementation of the default servlet in this case, you can use this simple servlet to wrap request to the container's implementation:

    
    package com.example;
    
    import java.io.*;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class DefaultWrapperServlet extends HttpServlet
    {   
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
        {
            RequestDispatcher rd = getServletContext().getNamedDispatcher("default");
    
            HttpServletRequest wrapped = new HttpServletRequestWrapper(req) {
                public String getServletPath() { return ""; }
            };
    
            rd.forward(wrapped, resp);
        }
    }
    

提交回复
热议问题