Roman Roman - 1 year ago 78
Java Question

What's the difference between getRequestURI and getPathInfo methods in HttpServletRequest?

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.

On my local machine

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?

Answer Source

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

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 (/).

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


Would result in:

RequestURI = /foo/path/to/resource


PathInfo = /path/to/resource
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download