2

After the original call to servlet.doGet() has returned, asyncContext.getRequest().getServletContext() seems to return null. How do I get the ServletContext in a plain Java method given only the AsyncEvent or AsyncContext? I. e. outside the scope of a doGet(), doPost() or one of the other servlet methods?

necromancer
  • 23,916
  • 22
  • 68
  • 115

1 Answers1

1

I do not know of a way to get the ServletContext out of these objects thus I would go with an approach of setting it directly yourself.

Similar approach I use in this answer where I am setting the context for a Quartz Scheduler's job.

If in your case it is your regular Java class just pass it on creation or have a setter method.

Community
  • 1
  • 1
Boro
  • 7,913
  • 4
  • 43
  • 85
  • In my case it is an anonymous inner class for which I instantiate an outer doGet/doPost method final variable which is initialized to the ServletContext from the servlet's getServletContext() method. Works fine, and from your answer it looks like there is no other way. Thanks! – necromancer Apr 25 '12 at 20:33