Results 1 to 6 of 6

Thread: Accessing the request object

  1. #1

    Default 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 at 05:32 AM.

  2. #2
    Join Date
    Apr 2005
    Location
    San Francisco, CA
    Posts
    1,224

    Default

    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.
    Jeremy Grelle

    Staff Engineer, Web Products Team
    SpringSource

  3. #3

    Default

    Quote 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>

  4. #4
    Join Date
    Apr 2005
    Location
    San Francisco, CA
    Posts
    1,224

    Default

    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?
    Jeremy Grelle

    Staff Engineer, Web Products Team
    SpringSource

  5. #5

    Default

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

  6. #6
    Join Date
    Apr 2005
    Location
    San Francisco, CA
    Posts
    1,224

    Default

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

    Staff Engineer, Web Products Team
    SpringSource

Posting Permissions

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