Some thoughts on JSF…
Developed through the Java Community Process (JCP) by a group of technology leaders, including Sun Microsystems later acquired by Oracle, Borland, BEA, and IBM, along with a collection of Java and Web experts well known in the industry under JSR – 314, Java Server Faces technology establishes the standard for building server-side user interfaces. With the contributions of the expert group, the JavaServer Faces APIs are being designed so that they can be leveraged by tools that will make web application development even easier.
What is JSF?
Table of Contents
JSF is a request-driven MVC web framework based on component-driven UI design model, using XML files called view templates or Facelets views. Requests are processed by the FacesServlet, which loads the appropriate view template, builds a component tree, processes events, and renders the response (typically HTML) to the client. The state of UI components (and some other objects) is saved at the end of each request (called state saving), and restored upon next creation of that view. Several types of state-saving are available, including client-side and server-side state saving. Out of the box, JSF 1.x uses Java Server Pages (JSP) for its display technology, but can also accommodate other technologies (such as XUL and Facelets). JSF 2 uses Facelets by default for this purpose. Facelets is a more efficient, simple, and yet more powerful view description language (VDL).
What’s the difference between JSF and JSP/Servlets?
JSP (Java Server Pages) is a Java view technology running on the server machine which allows you to write template text in (the client side languages like HTML, CSS, JavaScript and so on). JSP supports the so-called tag libraries shortly denoted as taglibs which are backed by pieces of Java code with which you can control the page flow and/or output dynamically (programmatically). A well known taglib is JSTL. JSP also supports the Expression Language which can be used to access back-end data (actually, the attributes which are available in page, request, session and application scopes), mostly in combination with taglibs.
When a JSP is requested for the first time or when the web application starts up, the servlet container will compile it into a servlet, which is a class extending HttpServlet and use it during the lifetime of the web application. In Tomcat, for example, you can find the source code of the generated servlet in the server’s work directory. On a JSP request, the servlet container will execute the compiled JSP class and send the generated output (usually just HTML/CSS/JS) through the web server over the network to the client side which in turn displays it in the web browser.
Servlets
Servlet is a Java application programming interface (API) running on the server machine which can intercept on the requests made by the client and can generate/send a response accordingly. A well known example is the HttpServlet which provides methods to hook on HTTP requests using the popular HTTP methods such as GET and POST. You can configure HttpServlets to listen on a certain HTTP URL pattern, which is configurable in web.xml, or more recently with Java EE 6, with @WebServlet annotation (in accordance with the Servlet 3.0 specification).
When a Servlet is requested for the first time or when the web application starts up, the servlet container will create an instance of it and keep it in memory during the web app’s life cycle. The same instance will be reused for every incoming request whose URL matches the servlet’s URL pattern. You can access the request data by HttpServletRequest and handle the response by HttpServletResponse. Both objects are available as method arguments inside any of the overridden methods of HttpServlet, such as doGet() and doPost().
Enter JSF (Java Server Faces)
JSF is a component based MVC framework which is built on top of the Servlet API and provides components in flavor of taglibs which can be used in JSP or any other Java based view technology such as Facelets. Facelets is much more suited to JSF than JSP. It namely provides great templating capabilities such as composite components, while JSP basically only offers the for templating, so that you’re forced to create custom components with raw Java code (which is a bit opaque and a lot of tedious work in JSF) when you want to replace a repeated group of components by a single component. If you can, I recommend to drop JSP and go for Facelets when you want to develop with JSF.
As being a MVC (Model-View-Controller) framework, JSF provides the FacesServlet as the sole request-response Controller. It takes all the standard and tedious HTTP request/response work from your hands, such as gathering user input, validating/converting them, putting them in model objects, invoking actions and rendering the response. This way you end up with basically a JSP or Facelets (XHTML) page for View and a Javabean class as Model. The JSF components are been used to bind the view with the model (such as your ASP.NET web control does) and the FacesServlet uses the JSF component tree to do all the work.
Why Java Server Faces?
Here are the advantages of JSF:
Nowadays, you can choose among many frameworks for developing the user interface of a web application. Java Server Faces (JSF) is a component-based framework. For example, if you want to display a table with rows and columns, you do not generate HTML tags for rows and cells in a loop, but you add a table component to a page. (If you are familiar with client-side Java development, you can think of JSF as “Swing for server-side applications.”) By using components, you can think about your user interface at a higher level than raw HTML. You can reuse your own components and use third-party component sets. And you have the option of using a visual development environment in which you can drag and drop components onto a form.
JSF offers:
- A set of prefabricated UI (user interface) components
- An event-driven programming model
- A component model that enables third-party developers to supply additional components
Some JSF components are simple, such as input fields and buttons. Others are quite sophisticated—for example, data tables and trees.
JSF contains all the necessary code for event handling and component organization. Application programmers can be blissfully ignorant of these details and spend their effort on the application logic.
JSF is not the only component-based web framework, but it is the view layer in the Java EE standard. JSF is included in every Java EE application server, and it can be easily added to a standalone web container such as Tomcat.
Unlike most web frameworks, JSF is a standard with multiple implementations (and as stated earlier, it is Oracle’s reference implementation which has also become a Java EE standard). This gives you a choice of vendors. Another advantage is that a standards committee has given considerable thought to the design of the framework, and that JSF is continuously improved and updated.
JSF 2.0 is much simpler to use than JSF 1.x, and it provides new and powerful features, such as easy Ajax integration and composite component authoring, built-in handling of http GET requests and page navigation improvements.
Example JSF Code / Tutorial
See my next post for a simple JSF tutorial. It teaches the reception and parsing of JSON objects containing exchange rates for certain currencies from a service provider and how to display them on an xhtml web page using AJAX.