views:

596

answers:

2

I have define a Spring bean.

<beans>
  <bean id="remoteService" class="edu.wustl.catissuecore.CaTissueApplictionServicImpl" />
</beans>

Is there any way to get the IP address of client in this class? Similarly as available in the servlet request.getRemoteAddr();

A: 

It depends on what kind of bean you have. If you have an AbstactController you have access to the HttpServletRequest object in the handleRequest method.

kgiannakakis
No my bean is not inheriting AbstarctController. The bean is : <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org /dtd/spring-beans.dtd"> <beans> <bean id="remoteService" class="gov.nih.nci.system.comm.server.ApplicationServiceServerImpl" /> </beans>But i am not getting RequestContextHolder in my classpath. I am using spring 1.2.7
Sachin
+1  A: 

The simplest (and ugliest) approach is to use RequestContextHolder:

  String remoteAddress = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())
       .getRequest().getRemoteAddr();

Without knowing more about your bean and how it's wired up, that's the best I can suggest. If your bean is a controller (either subclassing AbstractController or being annotated with @Controller) then it should be able to get direct access to the request object.

skaffman
No my bean is not inheriting AbstarctController. The bean is : <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org /dtd/spring-beans.dtd"> <beans> <bean id="remoteService" class="gov.nih.nci.system.comm.server.ApplicationServiceServerImpl" /> </beans>But i am not getting RequestContextHolder in my classpath. I am using spring 1.2.7
Sachin