ServletContext.getRequestDispatcher() vs ServletRequest.getRequestDispatcher()

后端 未结 6 1897
别跟我提以往
别跟我提以往 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:26

    I would think that your first question is simply a matter of scope. The ServletContext is a much more broad scoped object (the whole servlet context) than a ServletRequest, which is simply a single request. You might look to the Servlet specification itself for more detailed information.

    As to how, I am sorry but I will have to leave that for others to answer at this time.

    0 讨论(0)
  • 2020-12-02 16:27

    request.getRequestDispatcher(“url”) means the dispatch is relative to the current HTTP request.Means this is for chaining two servlets with in the same web application Example

    RequestDispatcher reqDispObj = request.getRequestDispatcher("/home.jsp");
    

    getServletContext().getRequestDispatcher(“url”) means the dispatch is relative to the root of the ServletContext.Means this is for chaining two web applications with in the same server/two different servers

    Example

    RequestDispatcher reqDispObj = getServletContext().getRequestDispatcher("/ContextRoot/home.jsp");
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-02 16:35

    The request method getRequestDispatcher() can be used for referring to local servlets within single webapp.

    Servlet context based getRequestDispatcher() method can used of referring servlets from other web applications deployed on SAME server.

    0 讨论(0)
  • 2020-12-02 16:41

    I think you will understand it through these examples below.

    Source code structure:

    /src/main/webapp/subdir/sample.jsp
    /src/main/webapp/sample.jsp
    

    Context is: TestApp
    So the entry point: http://yourhostname-and-port/TestApp


    Forward to RELATIVE path:

    Using servletRequest.getRequestDispatcher("sample.jsp"):

    http://yourhostname-and-port/TestApp/subdir/fwdServlet  ==> \subdir\sample.jsp
    http://yourhostname-and-port/TestApp/fwdServlet ==> /sample.jsp
    

    Using servletContext.getRequestDispatcher("sample.jsp"):

    http://yourhostname-and-port/TestApp/subdir/fwdServlet ==> java.lang.IllegalArgumentException: Path sample.jsp does not start with a "/" character
    http://yourhostname-and-port/TestApp/fwdServlet ==> java.lang.IllegalArgumentException: Path sample.jsp does not start with a "/" character
    


    Forward to ABSOLUTE path:

    Using servletRequest.getRequestDispatcher("/sample.jsp"):

    http://yourhostname-and-port/TestApp/subdir/fwdServlet  ==> /sample.jsp
    http://yourhostname-and-port/TestApp/fwdServlet ==> /sample.jsp
    

    Using servletContext.getRequestDispatcher("/sample.jsp"):

    http://yourhostname-and-port/TestApp/subdir/fwdServlet ==> /sample.jsp
    http://yourhostname-and-port/TestApp/fwdServlet ==> /sample.jsp
    
    0 讨论(0)
  • 2020-12-02 16:43

    Context is stored at the application level scope where as request is stored at page level i.e to say

    Web Container brings up the applications one by one and run them inside its JVM. It stores a singleton object in its jvm where it registers anyobject that is put inside it.This singleton is shared across all applications running inside it as it is stored inside the JVM of the container itself.

    However for requests, the container creates a request object that is filled with data from request and is passed along from one thread to the other (each thread is a new request that is coming to the server), also request is passed to the threads of same application.

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