ServletContext.getRequestDispatcher() 与 ServletRequest.getRequestDispatcher() [英] ServletContext.getRequestDispatcher() vs ServletRequest.getRequestDispatcher()

查看:20
本文介绍了ServletContext.getRequestDispatcher() 与 ServletRequest.getRequestDispatcher()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么

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

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

哪里

getRequestDispatcher(String path) ofServletContext 可以使用getContext(String uripath) 方法获取资源的RequestDispatcher在外国语境中.

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

以及如何?

请帮忙

推荐答案

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

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

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

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

例如,如果您在 http://example.com/myapp/subdir 上收到您的请求,

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

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

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

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

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

这篇关于ServletContext.getRequestDispatcher() 与 ServletRequest.getRequestDispatcher()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆