ServletContext.getRequestDispatcher() vs ServletRequest.getRequestDispatcher()

后端 未结 6 1910
别跟我提以往
别跟我提以往 2020-12-02 15:58

why

getRequestDispatcher(String path) of the ServletRequest interface cannot extend outside the current servlet context

<
6条回答
  •  爱一瞬间的悲伤
    2020-12-02 16:28

    If you use an absolute path such as ("/index.jsp"), there is no difference.

    If you use relative path, you must use HttpServletRequest.getRequestDispatcher(). ServletContext.getRequestDispatcher() doesn't allow it.

    For example, if you receive your request on http://example.com/myapp/subdir,

        RequestDispatcher dispatcher = 
            request.getRequestDispatcher("index.jsp");
        dispatcher.forward( request, response ); 
    

    Will forward the request to the page http://example.com/myapp/subdir/index.jsp.

    In any case, you can't forward request to a resource outside of the context.

提交回复
热议问题