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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
bazdmeg
Guest
bazdmeg

Kurva anyád

trackback
Night Coder | How to show current version in an JBoss application

[…] Edit: I found a much simpler solution to the problem of getting hold of the servlet context (link) thanx to this blog: http://www.mkyong.com/jsf2/how-to-get-servletcontext-in-jsf-2/ […]