Java ServletContext.getRequestDispatcher() 与 ServletRequest.getRequestDispatcher()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1411114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:47:38  来源:igfitidea点击:

ServletContext.getRequestDispatcher() vs ServletRequest.getRequestDispatcher()

javaservlets

提问by JavaResp

why

为什么

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

ServletRequest 接口的 getRequestDispatcher(String path) 不能扩展到当前 servlet 上下文之外

where as

然而

getRequestDispatcher(String path) of the ServletContext can use the getContext(String uripath) method to obtain RequestDispatcher for resources in foreign contexts.

ServletContext 的 getRequestDispatcher(String path) 可以使用 getContext(String uripath) 方法获取外部上下文资源的 RequestDispatcher。

and how??

如何??

Please help

请帮忙

采纳答案by ZZ Coder

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

如果使用绝对路径,例如 ( "/index.jsp"),则没有区别。

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

如果使用相对路径,则必须使用HttpServletRequest.getRequestDispatcher(). ServletContext.getRequestDispatcher()不允许。

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

例如,如果您在 上收到您的请求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.

将请求转发到页面http://example.com/myapp/subdir/index.jsp

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

在任何情况下,您都不能将请求转发到上下文之外的资源。

回答by cjstehno

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.

我认为你的第一个问题只是范围问题。ServletContext 是一个比 ServletRequest 范围更广的对象(整个 servlet 上下文),后者只是一个请求。您可以查看 Servlet 规范本身以获取更详细的信息。

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

至于怎么做,我很抱歉,但我现在不得不把它留给其他人来回答。

回答by Rajat

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.

Web Container 将应用程序一一启动并在其 JVM 中运行。它在其 jvm 中存储一个单例对象,并在其中注册放置在其中的任何对象。该单例在其中运行的所有应用程序之间共享,因为它存储在容器本身的 JVM 中。

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.

然而,对于请求,容器创建一个请求对象,该对象填充来自请求的数据,并从一个线程传递到另一个线程(每个线程都是一个新的请求,即将到达服务器),请求也传递给相同的应用程序。

回答by shraddha

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

request 方法getRequestDispatcher()可用于在单个 web 应用程序中引用本地 servlet。

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

基于 Servlet 上下文的getRequestDispatcher()方法可用于从部署在同一服务器上的其他 Web 应用程序中引用 servlet 。

回答by Sachindra N. Pandey

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

request.getRequestDispatcher(“url”) 表示分派是相对于当前 HTTP 请求的。表示这是为了在同一个 web 应用程序中链接两个 servlet 示例

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

getServletContext().getRequestDispatcher("url") 表示分派是相对于 ServletContext 的根的。表示这是为了将两个 Web 应用程序链接到同一服务器/两个不同的服务器中

Example

例子

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