HttpServletRequest 中的 getRequestURI 和 getPathInfo 方法有什么区别? [英] What's the difference between getRequestURI and getPathInfo methods in HttpServletRequest?

查看:15
本文介绍了HttpServletRequest 中的 getRequestURI 和 getPathInfo 方法有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个简单、非常轻便的前端控制器.我需要将请求路径与不同的处理程序(操作)匹配,以便选择正确的处理程序.

I'm making a simple, very lightweight front-controller. I need to match request paths to different handlers (actions) in order to choose the correct one.

在我的本地机器上HttpServletRequest.getPathInfo()HttpServletRequest.getRequestURI() 返回相同的结果.但我不确定他们会在生产环境中返回什么.

On my local machine HttpServletRequest.getPathInfo() and HttpServletRequest.getRequestURI() return the same results. But I'm not sure what will they return in the production environment.

那么,这些方法有什么区别,我应该选择什么?

So, what's the difference between these method and what should I choose?

推荐答案

getPathInfo() 在 URI 后面给出了额外的路径信息,用于访问你的 Servlet,其中 getRequestURI() 给出完整的 URI.

getPathInfo() gives the extra path information after the URI, used to access your Servlet, where as getRequestURI() gives the complete URI.

我原以为它们会有所不同,因为首先必须为 Servlet 配置自己的 URI 模式;我不认为我曾经从根 (/) 服务过 Servlet.

I would have thought they would be different, given a Servlet must be configured with its own URI pattern in the first place; I don't think I've ever served a Servlet from root (/).

例如,如果 Servlet 'Foo' 映射到 URI '/foo' 那么我会想到 URI:

For example if Servlet 'Foo' is mapped to URI '/foo' then I would have thought the URI:

/foo/path/to/resource

会导致:

RequestURI = /foo/path/to/resource

PathInfo = /path/to/resource

这篇关于HttpServletRequest 中的 getRequestURI 和 getPathInfo 方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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