I just started learning JSF.
While working with an example, I felt the need to access the ServletContext object inside MyBean class. I wanted to use an object which I had put inside the ServletContext using Listener. Can I do that? Does ServletContext has its scope inside Beans as well?
It’s just available by
ExternalContext#getContext(). See also its javadoc:So, this should do:
Unrelated to the concrete question, depending on the concrete functional requirement, this may not be the right solution to the concrete problem. General consensus is that your JSF code should be as much as possible free of any
javax.servlet.*dependencies/imports. Your question isn’t exactly clear, but if you indeed intend to access an attribute which you’ve put in the servlet context, then just get it fromExternalContext#getApplicationMap()instead.E.g. in the
ServletContextListener:and then in JSF
or even just by
@ManagedProperty