How to get ServletContext in JSF 2

The ServletContext class is important in web application, often times, you need this class get the information of current deployed servlet container.

Here’s a trick to show you how to get ServletContext in JSF2, via FacesContext.


ServletContext servletContext = (ServletContext) FacesContext
    .getCurrentInstance().getExternalContext().getContext();

References

  1. ServletContext JavaDoc
  2. FacesContext JavaDoc

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
bazdmeg
9 years ago

Kurva anyád