A photo of Geoffrey Hayward

Find the Context Path with JSF

Published September 22, 2015

Illustration of a laptop displaying Java code related to finding the context path with JSF, set against a web development-themed background.

Sometimes JSF does not have a component that will produce a particular type of HTML element. That’s not a problem but, I always forget the three method deep route to the context path. I always find I have to work through an IDE’s code completion tool to find the application’s path.

Here it is for next time:

#{facesContext.externalContext.requestContextPath}

A shorter version:



#{request.contextPath}

Just for completeness here is the JSP version:



${pageContext.request.contextPath}

And finaly, the scriptlet version:



<%
    String root = pageContext.getRequest().getServletContext().getContextPath();
%>

If you know any more please do leave a comment.

Related Posts

How to Store Variables in JSF Facelets

September 7, 2015

JSF Facelets can store the returned value yielded from a call to an EJB. Doing so will mean the EJB does less work.

Continue reading