Spring provides the current HttpServletRequest object (as well as the current HttpSession object) through a wrapper object of type ServletRequestAttributes . This wrapper object is bound to ThreadLocal and is obtained by calling the static RequestContextHolder.currentRequestAttributes() method.
ServletRequestAttributes provides the getRequest() method to get the current getSession() request, to get the current session and other methods to get attributes stored in both areas. The following code, although a little ugly, should provide you with the current request object anywhere in the application:
HttpServletRequest curRequest = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()) .getRequest();
Note that the RequestContextHolder.currentRequestAttributes() method returns an interface and must be entered into the ServletRequestAttributes type that implements the interface.
Spring Javadoc: RequestContextHolder | ServletRequestAttributes
samitgaur Jul 24 '10 at 1:00 2010-07-24 01:00
source share