What's the difference between getRequestURI and getPathInfo methods in HttpServletRequest?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In the Java Servlet API, working with URL patterns and understanding the core methods available in the HttpServletRequest interface can significantly enhance the flexibility and power of a web application. Among these, getRequestURI() and getPathInfo() are often used to process incoming requests. Both play distinct roles and are utilized based on the requirements of URL manipulation or information retrieval from the request.
Understanding getRequestURI() and getPathInfo()
Both getRequestURI() and getPathInfo() are part of the HttpServletRequest interface. This interface serves to provide request-specific information for HTTP servlets, allowing developers to extract values from the URL and work with them programmatically.
1. getRequestURI()
- Description:
getRequestURI()retrieves the complete URI (Uniform Resource Identifier) of the request, which contains the URL path as well as any custom URL suffixes.- The URI includes the context path (path at which the web application is accessed) and the servlet path, and may also include additional path information.
- Example Usage:
Suppose the URL is http://example.com/myapp/widgets/list. Here, getRequestURI() would return /myapp/widgets/list.
- Use Case:
- When you need the entire path to manage routing, logging, or complex URL processing.
2. getPathInfo()
- Description:
getPathInfo()returns any extra path information that's part of a URL, which cannot be parsed into other defined components like context or servlet paths.- It essentially returns the remaining part of the URI beyond what has been mapped to the context path and the servlet path (the matched path for the servlet's mapping).
- Example Usage:
Given the URL http://example.com/myapp/widgets/list, if the servlet mapping is /widgets/*, then getPathInfo() would return /list.
- Use Case:
- Useful for dynamic segment parsing in RESTful services or when additional path information governs functionalities.
Key Differences and Summary Table
Below is a comparison of getRequestURI() and getPathInfo():
| Feature | getRequestURI() | getPathInfo() |
| Purpose | Retrieves the full request URI | Retrieves extra path info |
| Returns | Full path from root to end | Path after servlet mapping |
| Includes Context Path? | Yes | No |
| Includes Servlet Path? | Yes | No |
| Use Cases | Full URL manipulation, Logging | RESTful URI segment parsing |
Example Scenario
To better illustrate the difference between these two methods, consider an application where URLs follow a scheme like http://example.com/myapp/order/view/12345.
- Context Path:
/myapp - Servlet Path:
/order - Expected Functionality:
- Objective: Display order details based on order ID.
For this, you can use:
getRequestURI(): To log the full access path for security and analytics.getPathInfo(): To extractview/12345for business logic handling in the servlet.
Additional Details
- Servlet Mapping: You must be cognizant of how servlets are mapped in
web.xmlor via annotations. The fields affected by these mappings directly impact how paths are interpreted and methods likegetServletPath()work alongsidegetRequestURI()andgetPathInfo(). - Handling Edge Cases: If the mapping is different or if there's no extra path info,
getPathInfo()may returnnull. Thus, always consider null-checking the result ofgetPathInfo()before processing it further. - Use in RESTful Application Design:
getPathInfo()is particularly effective in RESTful designs, where URL segments represent resources or actions to be performed, contributing to clean and interpretable URLs.
Understanding these two methods enables developers to design sophisticated URL handling capabilities, catering to a wide range of web application requirements. While getRequestURI() provides a comprehensive overview, getPathInfo() allows for pinpointed operations, both serving critical roles in Java web application architecture.
Related reading
- What's the difference between Hibernate and Spring Data JPA
- What''s the difference between implementation, api and compile in Gradle?
- What's the difference between Instant and LocalDateTime?
- What's the difference between interface and @interface in java?
- What's the difference between Jetty and Netty?
- What's the difference between JPA and Hibernate?
- What's the difference between JPA and Spring Data JPA?
- What's the difference between kafka.javaapi.* and org.apache.kafka.*?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.