Announcement Announcement Module
Collapse
No announcement yet.
Accessing the request object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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

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

    Code:
    <listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    
    <servlet>
    	<servlet-name>springflex</servlet-name>
    	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    	<load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
    <servlet-name>springflex</servlet-name>
    <url-pattern>/messagebroker/*</url-pattern>
    </servlet-mapping>
    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(RequestContextHolder.java:123)
    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.

    Comment


    • #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.
      Code:
      public class DynamicDataSource extends AbstractRoutingDataSource {
      
      	@Autowired(required = true)
      	private HttpServletRequest request;
      
      	public HttpServletRequest getRequest() {
      		return request;
      	}
      
      
      	@Override
      	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();
      
      		if(tempURL=..)
      		return A;
      		if(tempURL=..)
      		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"/>
      	  </map>
         </property>
         <property name="defaultTargetDataSource" ref="A"/>
      </bean>

      Comment


      • #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?

        Comment


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

          Comment


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

            Comment

            Working...
            X