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
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
newest oldest most voted
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/ […]

bazdmeg
Guest
bazdmeg

Kurva anyád