Spring MVC 3.1 - How to Get HttpServletRequest and HttpServletResponse objects in Controller Method

When Spring MVC 3.1 dispatcher introspect the parameters lists of user defined controller method that is to be executed. It identifies it from URL pattern. Using reflections it introspect the types of parameters and pass the required object to that controller method. For example, there is a method hello() which expect HttpServletRequest and HttpServletResponse object binded or associated with current under-process request, the hello() method can be declared like this:

@Controller
@RequestMapping("/hello")
public class HelloController {

@RequestMapping(method = RequestMethod.GET)
public String hello(HttpServletRequest request, HttpServletResponse response) {

// Use request and response object as you in JSP or Servlets

}

So, the Spring MVC 3.1 would automatically pass the defined parameters.

Comments