Hi Chris,
HttpServletRequest is part of the standard Java servlet API, so using it does not tie you to an implementation. However, if you would like to get the client IP address without having to add HttpServletRequest as one of your method parameters you could use the following approach to isolate the HttpServletRequest to a single method on a controller:
Code:
@Controller
@RequestMapping("/some.action")
public final class SomeController {
@ModelAttribute("clientIpAddress")
public String populateClientIpAddress(
HttpServletRequest request) {
return request.getRemoteAddr();
}
@RequestMapping(method = RequestMethod.GET)
public String handleRequest(
@ModelAttribute("clientIpAddress") String clientIpAddress,
ModelMap model) throws Exception {
// handle request without referencing servlet API
return "view";
}
}