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?
I saw some examples creating the JSF pages with .jsp extension, other examples creating
Share
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
*.jsfis just one of widely used URL patterns of theFacesServletmapping inweb.xml. Other ones are*.facesand/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 theFacesServletwill be invoked this way. When using JSPs, it would actually executepage.jsp. When using Facelets, this would actually compilepage.xhtml.Since JSF 2.x you can also use
*.xhtmlas URL pattern. This way you don’t need to get confused when specifying URLs. Using*.xhtmlas URL pattern was not possible in JSF 1.x with Facelets 1.x, because theFacesServletwould then run in an infinite loop calling itself everytime. An additional advantage of using*.xhtmlis 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.jsfto.xhtml. It is not possible to use*.jspas URL pattern, because this way the container’s builtinJspServlet, which is already using that URL pattern, would be overridden and then theFacesServletwouldn’t be able to feed on JSPs anymore.See also: