From the course: Complete Guide to Spring MVC

Relative servlet requests - Spring Tutorial

From the course: Complete Guide to Spring MVC

Relative servlet requests

- [Lecturer] Relative Servlet Requests. Relative Servlet Requests allow you to dynamically construct URIs based on the structure of the current http requests. This feature is particularly useful when building context-aware links, handling redirects, or working with APIs that require dynamic URI generation. How Do Relative Servlet Requests Help You? First, you have Dynamic Linking. It generate links that adapt to the current request structure, meaning its scheme, host, port, and path, which we'll see an example of shortly. It's Context Aware. It builds URIs relative to the application's context path or servlet mapping without hardcoding. Ease of Maintenance. You avoid manually constructing full URLs, reducing errors and improving readability. Let's go through a few scenarios and examples. Building URIs Relative to the Current Request. This approach reuses parts of the current request like scheme, host, port, and path, while allowing modifications. If the current request is example.com/users?role=admin, what will our generated URI be based off of the sample code that we have here? You should see that we're going to replace id with 123, So our generated URL should be example.com/users?role=admin and account id equals 123. You'll want to use this if you need to modify or add query parameters dynamically without hardcoding the entire URI structure. Next is Building URIs Relative to the Application's Context Path. This approach constructs URIs that respect the application's root context path. It's ideal for internal resource links or API calls. So in this scenario, if the Application's Context Path is forward slash app, and the current request is forward slash app, forward slash any-path, the generated URI will be... What do you think? We are going to replace app in any-path with accounts. So we're generating application specific URIs that align with the application's root context. The third example is Building URIs Relative to a Servlet Mapping. When your application uses specific Servlet Mappings, you can generate URIs that include the Servlet's prefects. If the Servlet Mapping is forward slash main, and the current request is example.com/main/any-path, the generated URI will be example.com/app/main/accounts. You'll want to use this if you want to construct URIs for Servlet specific endpoints dynamically. The fourth example is Handling Forwarded Headers. And distributed systems requests often pass through proxies or load balancers. Headers like Forwarded and X-Forwarded, provide details about the original client request. Spring ignores these headers by default for security reasons, but you can enable them using ForwardedHeaderFilter. In this example, we are adding a ForwardHeadedFilter, @Bean, to your application. The result is that now, ServletUriComponentsBuilder will consider these headers when constructing URIs. So within IntelliJ, let's add an additional method to our application. In this example, we are redirecting dynamically to a greeting page. So go ahead and copy the controller code that I have here for what I'm calling, Redirect to Greeting. And what we want to happen is that we want the browser to send a get request to our spring application using the redirect endpoint. If our greeting endpoint isn't implemented yet, you'll get a 404 not found error when the browser tries to follow the redirect. To test the redirect logic without relying on the browser, you can use unit test or MockMvc. If the application context is forward slash app, and the request that we'll use is localhost.8080/redirect, then the redirect URI should be localhost.8080/greeting?name=Spring, based off of the method that we have added here. So when you go to your browser and you input localhost.8080/redirect, you should receive a dynamically generated URL. What we have learned in spring MVC about Relative Servlet Requests is that you can dynamically construct URIs at respect the current request context. You can avoid hardcoding, leading to cleaner and more maintainable code. You can build secure and flexible applications that adapt to various appointment scenarios, like behind proxies or load balancers. These techniques ensure your spring applications are robust, dynamic, and easy to extend as they grow.

Contents