Wrap the default servlet but override the default webapp path

后端 未结 6 1932
故里飘歌
故里飘歌 2020-12-10 20:33

I have a folder of static html,imgs,flash content that lives outside of the webapp folder. Right now I\'m using a symbolic link to map that folder into my webapp directory.

6条回答
  •  暖寄归人
    2020-12-10 21:31

    You can change to a different path within your webapp context. Here's an example which does differential serving depending on whether the client's User-Agent supports ES6:

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RequestDispatcher rd = getServletContext().getNamedDispatcher("default");
        HttpServletRequest wrapped = new HttpServletRequestWrapper(req) {
            @Override
            public String getServletPath() {
                String prefix = supportsES6(req) ? "/es6" : "/es5";
                String newPath =  prefix + req.getServletPath();
                if (newPath.endsWith("/")) newPath += "index.html";
                return newPath;
            }
        };
        rd.forward(wrapped, resp);
    }
    

    However, "es5" and "es6", even though we use the initial slash, are subdirectories of the webapp's ordinary context. It's not possible to break outside of the context directory using this method.

提交回复
热议问题