Access HttpServletRequest and HttpServletResponse in Spring MVC controller

In some cases, you will need to access the HttpServletRequest and HttpServletResponse objects in the Spring MVC controller when processing a request URL. At that point, you need to simply add two variables referencing these two objects as parameters of the method.

In the example of the previous tutorial, we can add two more arguments to the home() method and log some information like this:

At this point, Spring MVC will automatically put these two objects into our method and thus you can access all the properties of these two objects.

Result:

One thought on “Access HttpServletRequest and HttpServletResponse in Spring MVC controller

  1. Hi , I have a question here , How do we use this HttpServletResponse object in a class which is calling this controller?

Add Comment