Announcement Announcement Module
Collapse
No announcement yet.
How to get HttpServletRequest via ApplicationContext? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get HttpServletRequest via ApplicationContext?

    Hello,

    I have a bean that has no knowledge of HttpServletRequest, but this bean implements ApplicationContextAware. How can it access HttpServletRequest via ApplicationContext? I failed to find a way to do this. If it is not possible, what other ways do you suggest to access HttpServletRequest?

    Best,
    David

  • #2
    I think you are a little confused about how web requests work. The application context can be used to retrieve objects managed by Spring. HttpServletRequests are objects generated for each web request, and are not created or managed by Spring. So you can't get the current request object from the application context.

    Create a class which contains a ThreadLocal. Then create a filter which kicks in for /* and places the HttpServletRequest object in the ThreadLocal. Then wire the class containing the ThreadLocal to the classes which needs the request. Now it will be available wherever you need it.

    jh

    Comment


    • #3
      Your explanation is very clear. Thanks! -David

      Comment


      • #4
        Do you have an example that you can share with us? Thanks!

        Comment

        Working...
        X