Announcement Announcement Module
No announcement yet.
Accessing the request object Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing the request object

    I am trying to get the request object so that I could get the application URL outside of the dispatch servlet. I tried all three methods below and it returned null.

    Is there any other way to get the request object from the RequestContextListener

    ServletRequestAttributes r = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
    HttpServletRequest req = r.getRequest(); 
    HttpServletRequest req = FlexContext.getHttpRequest();
    private HttpServletRequest req; 
    String completeURL = req.getRequestURL().toString() + "?" + req.getQueryString();
    My web.xml has the following code.

    Caused by: 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.
    	at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(
    Last edited by Java Developer; Dec 14th, 2010, 05:32 AM.

  • #2
    At what point in the request chain are you trying to execute that code?

    The FlexContext version definitely wouldn't work if you aren't processing the request through the DispatcherServlet, but the first method should work just fine as long as you are within the scope of a request.


    • #3
      Originally posted by jeremyg484 View Post
      At what point in the request chain are you trying to execute that code?
      I am not sure at which point of the request chain the call gets executing. I will list out the rest of the source, hope that answers this question.
      public class DynamicDataSource extends AbstractRoutingDataSource {
      	@Autowired(required = true)
      	private HttpServletRequest request;
      	public HttpServletRequest getRequest() {
      		return request;
      	protected Object determineCurrentLookupKey() {
      		// Get URL, determine DB type
      		// First attempt to get the complete URL
      		ServletRequestAttributes t = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
      		HttpServletRequest req = t.getRequest();
      		// Second attempt to get the complete URL
      		HttpServletRequest req = FlexContext.getHttpRequest();
      		// Third attempt to get the complete URL
      //		@Autowired
      //		private HttpServletRequest req;
      		String tempURL = req.getRequestURL().toString() + "?" + req.getQueryString();
      		return A;
      		return B;
      <bean id="dynamicSource" class="com.test.DynamicDataSource">
         <property name="targetDataSources">
      	  <map key-type="com.test.RoutingDataSource">
      		 <entry key="A" value-ref="datasourceA"/>
      		 <entry key="B" value-ref="datasourceB"/>
         <property name="defaultTargetDataSource" ref="A"/>


      • #4
        Ok, more specifically then, how is the determineLookupKey method actually getting invoked? Is it as the result of a remoting request or something entirely different?


        • #5
          The Spring Container calls determineLookupKey on server startup since I have defined an AbstractRoutingDataSource.


          • #6
            You can only get access to an HttpServletRequest object within the context of an actual HTTP request, not at container startup.