ServletRequest 对象的 getRequestDispatcher 和 ServletContext 对象的 getRequestDispatcher 的区别。

ServletContext 对象 的 getRequestDispatcher (java.lang.String path)  的官方文档

The pathname must begin with a "/" and is interpreted as relative to the current context root. 

路径名必须以 “ / ” 开始,路径相对于在当前上下文的根目录之下

比如,在 web.xml 中,有

<servlet-mapping>
      <servlet-name>ServletTest123</servlet-name>
      <url-pattern>/comment/ServletTest123</url-pattern>

</servlet-mapping>

path 应为 /comment/ServletTest123

ServletRequest 对象的 getRequestDispatcher (java.lang.String path)  的官方文档

The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher.

路径如果以 “ / ” 开始,路径相对于在当前上下文的根目录之下,否则则视为相对于当前 servlet 路径的父目录下

比如,当前servlet 名称是 ServletTest123 ,在 web.xml 中,有

<servlet-mapping>
      <servlet-name>ServletTest123</servlet-name>
      <url-pattern>/comment/ServletTest123</url-pattern>

</servlet-mapping>

 

<servlet-mapping>
      <servlet-name>ServletTest456</servlet-name>
      <url-pattern>/comment/ServletTest456</url-pattern>

</servlet-mapping>

现在通过 ServletRequest 对象 获取 servletTest456 的  RequestDispatcher 对象

第一种 path参数:("/comment/ServletTest456")

第二种 path参数:("ServletTest456")

阐述两者区别  的官方文档

The difference between this method and ServletContext.getRequestDispatcher(java.lang.String) is that this method can take a relative path.

 ServletRequest  和 ServletContext 的 getRequestDispatcher(java.lang.String) 方法的区别是 ServletRequest  既支持

相对于在当前上下文的根目录的参数(以 "/" 开头),还支持 相对于当前 servlet 路径的父目录 的参数,而 ServletContext只支持 相对于在当前上下文的根目录的参数(以 "/" 开头)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值