I saw some examples creating the JSF pages with .jsp
extension, other examples creating them with .xhtml
extension, and other examples choose .jsf
.
I just would like to know what the difference is between above extensions when working with JSF pages, and how to choose the appropriate extension?
JSP is an old view technology and widely used in combination with JSF 1.x. Facelets (by some people overgeneralized as XHTML) is the successor of JSP and introduced as default view technology of JSF 2.x at end of 2009. When you were seeing JSPs, you were perhaps reading outdated books, tutorials or resources targeted on JSF 1.x. You should generally ignore them when developing with JSF 2.x and head to resources targeted on JSF 2.x, otherwise you may end up in confusion because many things are done differently in JSF 2.x on Facelets.
The *.jsf
is just one of widely used URL patterns of the FacesServlet
mapping in web.xml
. Other ones are *.faces
and /faces/*
, but those are from back in the JSF 1.0/1.1 ages. They all do not represent the concrete file extension/path, but just a virtual file extension/path and is to be specified in URLs only like so http://example.com/contextname/page.jsf. If you are familiar with basic Servlets, then you should know that the servletcontainer will invoke the servlet when the request URL matches the servlet's URL pattern. So when the request URL matches *.jsf
, then the FacesServlet
will be invoked this way. When using JSPs, it would actually execute page.jsp
. When using Facelets, this would actually compile page.xhtml
.
Since JSF 2.x you can also use *.xhtml
as URL pattern. This way you don't need to get confused when specifying URLs. Using *.xhtml
as URL pattern was not possible in JSF 1.x with Facelets 1.x, because the FacesServlet
would then run in an infinite loop calling itself everytime. An additional advantage of using *.xhtml
is that the enduser won't be able to see raw JSF source code whenever the enduser purposefully changes the URL extension in browser address bar from for example .jsf
to .xhtml
. It is not possible to use *.jsp
as URL pattern, because this way the container's builtin JspServlet
, which is already using that URL pattern, would be overridden and then the FacesServlet
wouldn't be able to feed on JSPs anymore.