Results 1 to 3 of 3

Thread: getting ip address with annotation controllers while staying platform agnostic

  1. #1
    Join Date
    Jul 2008
    Posts
    6

    Default getting ip address with annotation controllers while staying platform agnostic

    hi there,

    we are using the amazing annotation based controller setup, which works like a treat - excellent work all round

    is there any way of retrieving the ip address of the request without resorting to HttpServletRequest objects (either directly as a parameter, or via NativeWebRequest) - this seems to tie us to a given servlet implementation, and we'd prefer to be completely platform-agnostic if possible

    thanks for any tips,
    chris

  2. #2
    Join Date
    Sep 2007
    Location
    Brooklyn, NY
    Posts
    20

    Default HttpServletRequest does not tie you to an implementation

    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";
    	}
    }

  3. #3
    Join Date
    Jul 2008
    Posts
    6

    Default

    ahhh, thanks for clearing that up. i thought that our reliance on catalina.jar (we use tomcat normally) was a problem, but we simply switched in the jboss jar instead and it worked just fine

    and thanks for the clever solution, @ModelAttribute absolutely rocks my world

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •