I think, therefore I blog

JSF java.io.FileNotFoundException: Not Found in ExternalContext as a Resource

By , 7 June 2011

JSF java.io.FileNotFoundException: Not Found in ExternalContext as a Resource

Really annoying. JSF 2/Facelets throws an exception for every request to resource which doesn't exist. You would have thought a 404 response would have made more sense. Here is the stack trace:

GRAVE: El Servlet.service() para servlet Faces Servlet lanzó una excepción
java.io.FileNotFoundException: /apps/foo.xhtml Not Found in ExternalContext as a Resource
	at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:224)
	at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:265)
	at com.sun.faces.facelets.impl.DefaultFaceletFactory.getMetadataFacelet(DefaultFaceletFactory.java:201)
	at com.sun.faces.application.view.ViewMetadataImpl.createMetadataView(ViewMetadataImpl.java:114)
	at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:227)
	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
	at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:111)
	at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
JSF java.io.FileNotFoundException: Not Found in ExternalContext as a Resource

It's good to have the exception for includes and invalid template paths, but this app is deployed publicly and has to handle all sorts of rubbish requests from bots, bad user agents and broken links.

I tried setting up an error page for this exception, but that didn't work possibly because JSF2 has 'fixed' exception handling with their custom ExceptionHandler. The only workaround for me was to hack my filter to check for Faces resources before the servlet can handle them.

// JSF logs annoying exceptions and if it can't find a given view
if (view.endsWith(".xhtml") && 
       !new File(context.getRealPath(view)).exists()) {
    response.sendError(404, "Cannot find " + view);
} else {
    request.getRequestDispatcher(view).forward(request, response);

About Roger Keays

JSF java.io.FileNotFoundException: Not Found in ExternalContext as a Resource

Roger Keays is an artist, an engineer, and a student of life. Since he left Australia in 2009, he has been living as a digital nomad in over 40 different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, and finding good food. Click here to subscribe to his weekly blog, or stalk him on Facebook and Twitter.

Leave a Comment

Please visit https://RogerKeays.com/jsf-java-io-filenotfoundexception-not-found-in-externalcontext-as-a-resource to add your comments.

Join 3,559 People Who Think Outside The Box

I write every Sunday about travel, psychology, technology, and all sorts of interesting stuff. It's completely free, and you can leave whenever you like. Subscribe now, and you'll be the first to get new articles each week.

Read a Good Book

“A spellbinding true story of love, passion and adventure. One can’t help but be swept away by 100% Love Guaranteed.” —Dr Tammie Matson, author of Elephant Dance.

Chat For A While

Your Vote Matters

Which animal will take over when humans go extinct?