Results 1 to 5 of 5

Thread: Get Remote IP Address from Spring

  1. #1
    Join Date
    Jan 2011
    Posts
    7

    Default Get Remote IP Address from Spring

    I need to get a Remote IP Address in my Spring Web app.

    Normally, this is done through the HttpServletRequest.getRemoteAddr() method. The question is how to get the HttpServletRequest object in Spring.

    I found this solution online:

    Code:
    String remoteAddress = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest().getRemoteAddr();
    and add the following listener in web.xml

    Code:
    <listener>
       <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    Unfortunately that doesn't work for me. I get this error:

    java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

    I'm not clear on what to do. Can anyone provide code that works? Thanks
    Last edited by eugenebalt; Jan 4th, 2011 at 09:34 AM.

  2. #2

    Default

    Hi eugenebalt,

    where are you trying to get the remote address? Is it in your JSP (view) or in your controller (servlet)?

    Are you using the DispatcherServlet?

  3. #3
    Join Date
    Jan 2011
    Posts
    7

    Default

    Thanks for the reply. Actually I'm using Wicket from within Spring. So it's more of a Wicket question. (I was trying to get the IP address inside its WebPage class.)

    I already found out how to do it in Wicket,

    (((WebRequest)getRequest()).getHttpServletRequest( ).getRemoteAddr())

  4. #4

    Default

    Hi,

    you might want to try you the contextFilter instead of the listener.
    Please try the following syntax in your web.xml file:

    Code:
    	<filter>
    		<filter-name>contextFilter</filter-name>
    		<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
    	</filter>
    
    	<filter-mapping>
    		<filter-name>contextFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    Use this filter as one of your first filters.

  5. #5
    Join Date
    Oct 2011
    Posts
    2

    Default

    Hi people. I am using Spring and ZK to the view in a e-learning application. So I want need to get the ip address of the client and lock if that ip is requesting the application for the second time.

    It is only allowed one request per IP address at time.

    How can I do this with spring?

    I have a DispatcherServlet.

    Please help.

Posting Permissions

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