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?
Asked
Active
Viewed 603 times
1 Answers
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.
-
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