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:

Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
3Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.