This tutorial provides frequently asked Java Server Faces or JSF interview questions and answers with explanations to help you prepare for the interview:
It is essential for Java Developer to have working knowledge in one or more popularly used Java frameworks like Spring, Hibernate, Struts, and Java Server Faces (JSF).
JSF is one of the Java frameworks that quickly help design web user interface components.
JSP was a Java technology that utilized Java API, implicit objects, and custom tags in order to develop dynamic web contents, whereas used Java Server Faces (JSF), a Java-based web application framework to develop web-based user interface components.
JSF has managed beans, face lets, annotations, and utility libraries such as prime faces and Omni faces or ice faces to develop server-side template-based components.
What You Will Learn:
Java Server Faces
Java Server Faces technology offers API to build reusable user interface components in the web pages. It connects components with the application data source and client-generated events to server-side event handlers, maintains UI state across multiple server requests as well as implements custom components.
JSF provides core library, standard HTML input elements as base UI components, based on various devices and client type say HTML browser, WAP or wireless devices, provides rendering capability to UI components.
Frequently Asked JSF Interview Questions
Q #1) What is Java Server Faces (JSF)?
Answer: It is a Java framework based on MVC design that has rich API and tag libraries that offer the creation of server-side user interface components for web development.
JSF has a tag library, API, Facelets, and UI components and managed beans to create web applications.
Q #2) What features Java Server Faces offer in web development?
Answer: The features are listed below:
- Java framework built over Servlet API
- UI components are stored on the server
- Event-driven programming model that is handled on the server
- Supports multiple client devices
- Ease of implementing 3rd party components
- Extensible architecture
- Extensive tool support from Borland, Exadel, Sun, Oracle, IBM, BEA, etc.
- Rapid application development (RAD) approach
- Supports internationalization of UI labels and text
Java Server Faces features:
Q #3) Explain JSF life cycle.
Answer: The life cycle contains 6 phases, as explained below:
#1) Restore view: The life cycle begins with the restore view phase. When a link or button is clicked on the web page, a request is sent to JSF with operations as listed below:
- View is built by JSF
- Connects event handlers and validators to user interface components
- Saves this View in FacesContext instance
- Provides all information to FacesContext to process request
#2) Apply request: Here, each component from the component tree is created. Use the decode method that captures and saves new values from request parameters. In case of any failure during conversion, it generates an error message and lists it on FacesContext. It will display validation messages or errors during the render response phase. JSF moves to render response phase when decode method event listeners call renderResponse method.
#3) Process validation: During this phase, JSF processes component tree validators, examines attribute rules for validation, and compares them with the stored local value of the component. In case of invalid local value, JSF adds an error message to FacesContext instance, displaying the same page with an error message and with JSF life cycle moving further towards render response phase.
#4) Model value update: After JSF verifies valid data, it sets the corresponding server-side object properties to the component’s local values and updates the bean properties, corresponding to the value attribute of the input component. When renderResponse from the current instance of FacesContext is being called by updateModels methods, JSF moves to the render response phase.
#5) Invoke application: Application-level events like submitting form or forwarding to another page are being handled by Java Server Faces during this phase.
#6) Render response: In case the application has JSP pages, JSF requests the application server to render the page, thereby adds components on the page to the component tree. Otherwise, an already built component tree need not add any components; JSP container move tags and renders components. The response state is saved after view content is rendered, making successive requests can access the state and its availability to restore view phase.
Q #4) What is the significance of managed beans in Java Server Faces?
Answer: Managed Beans are Java beans that comprise getter and setter methods and have business logic designed in it. It may optionally have a backing bean. They act as a Model for UI components in the MVC framework.
For JSF 1.2, it is required to register them in the JSF configuration file, faces-config.xml, whereas, from JSF 2.0 onwards, annotations are used to register managed beans.
Q #5) What are the scopes of managed beans?
Answer: Managed Beans are a Java class that consists of a set of getter/setter methods and properties. It acts as a model to the JSF framework and offers functionalities like,
- Component data validation
- Manage the execution of a component event
- Processing to decide what page application navigates next
- JSF use managed beans either by configuring them into an XML file or through annotations.
Following scopes are used for managed bean class:
Q #6) What is a Facelet?
Answer: Facelets are lightweight page declaration language which uses HTML style templates to build component tree to build Java server faces views.
Some of Facelets feature are listed as below:
- It uses XHTML for creating web pages.
- Besides JSF and JSTL tag libraries, it offers Facelets tag libraries.
- Expression language support, component, and pages templating.
Q #7) Explain the advantages of using Facelet.
Answer: The advantages are as follows:
- Fast compile time
- High-performance rendering
- Extends its functionality to components and other server-side objects via customization
- Support for code reuse through composite components and templating
- Compile-time EL validation
Q #8) List Facelet tags used in Java Server Face.
Answer: Some of the Facelet tags used in JSF are listed below:
- ui: component defines the created component and adds to the component tree.
- ui: composition defines page composition, uses templates optionally, and ignores content outside.
- ui: debug defines debug component created and adds to the component tree.
- ui: decorate is similar to component tag, consider content outside this tag.
- ui: define is used to define content inserted into the page by a template.
- ui: fragment is similar to component and decorates tag. It considers content outside this tag.
- ui: include reuses and encapsulates multiple page content.
- ui: insert as name inserts contents into a template.
- ui: param pass parameters to the included file.
- ui: repeat an alternative for loop tags, like c: forEach or c: dataTable.
- ui: remove is used to remove content from a page.
Q #9) List types of page navigation JSF supports.
Answer: JSF supports the following page navigation types:
- Conditional navigation
- Forward versus Redirect navigation
- Implicit navigation
- Navigation via managed bean
- Navigation via Redirect navigation
Q #10) Describe various text field tags offered by JSF.
Answer: Various text field tags are as described below:
- <h: inputText> inserts text box adjacent to label field.
- <h: inputTextArea> is used as a container for entering numerous characters.
- <h: inputSecret> is a type of text field that is used for password field so that the entered data is not revealed.
Q #11) Describe value expression and method expression.
Answer: Value expression retrieves a value and sets a value. They are further divided into rvalue expression and lvalue expression. rvalue can only read data, whereas lvalue can read as well write data.
Public method of the bean is invoked by Method expression that returns the result required for validating the data component and event handling.
Q #12) What types of validations are available in JSF?
Answer: There are two types of validations in JSF namely, Declarative and Imperative.
- Declarative validations are invoked using JSF standard validators or Bean validators.
- Imperative validations are complex user-declared validators that override the standard validation as standard validation messages are not adequate in all cases.
Q #13) List expressions JSF Expression Language supports.
Answer: Expressions supported are listed below:
- Immediate value expression
- Deferred value expression
- Value expression
- Method expression
Q #14) Describe various types of JSF events.
Answer: There are mainly three types of JSF events namely, Action events, Value Change events, and Phase events.
- Action events are generated for user interface components like the command button or command hyperlink.
- Value Change events reference text field, radio button list box. Any value change in these UI components will invoke a value change event.
- Phase events are fired either during the start or towards the end of any one of the six phases of the JSF life cycle.
Q #15) Explain in detail the data-bound table components.
Answer: Components responsible for displaying relational data in the tabular format are known as data-bound table components. <h: column> tag iterates across each of the record displayed in rows, whereas <h: dataTable> tag display data components.
Some of the attributes of h:dataTable are cellpadding, cellspacing, bgcolor, and border.
Q #16) List validator tags available in JSF.
Answer: The validator tags are listed below:
- f: validateLength is used to validate string length.
- f: validateLongRange is used while validating the numeric value range.
- f: validateDoubleRange is used while validating float value range.
- f: validateRegex: validation compares given regular expression with JSF components.
Q #17) What are JSF annotations?
Answer: Annotation in JSF helps in the development of clear and clean code. It can be used in packages, classes, constructors, interfaces, fields, methods, parameters, variables, or annotations themselves.
There are mainly three categories of annotation – viz. Markers, Single value annotation, Full annotations.
Q #18) Describe resource bundling.
Answer: It is always convenient to store UI labels, date format, status messages and UI textual elements such as storing parameters for locales for internationalization etc. at properties file instead of hard coding these in each XHTML page. Storing these constant values in one file and displaying these values on various pages is known as resource bundling.
Deployment descriptor faces-config.xml contains configuration settings for JSF application, where we can configure resource bundle and save these values in it.
Q #19) List the benefits of using the JSF framework.
Answer: Following are some of the important benefits:
- JSF framework quickly creates user interfaces with its built-in UI component library with a separation between presentation and behavior.
- It has a rich architecture for managing, processing, validating user inputs.
- Its events are integrated with server-side code.
- Offers pluggable component-level control.
- Supports accessibility and internationalization features.
Q #20) List the benefits of expression language.
Answer: Expression language offers the following benefits:
- Page authors can minimize the use of scripting in the web page
- Offer greater productivity
- Easy to maintain
- Flat learning curve
- Included in JSP 2.0, JSF
- Use of expression at different stages of the page life cycle
- Set external object data and get that data using expressions
- Method expressions offer event handling, validation, and other functionalities for JSF UI components
Q #21) Explain what Java Server Faces application war file directory may contain?
Answer: Web application archive (war) file of the JSF application contains the following:
- web.xml – a web application descriptor configuration resource for web applications.
- Essential classes in a set of JAR (Java Archive) files.
- Application classes, Java Server Faces pages, other required resources like image files.
- Resource files for application configuration.
Q #22) What types of Facelet tags are used in JSF 2.0?
Answer: Various Facelet tag types in JSF 2.0 are:
- Templates offer common interface style and layout in the web applications, ui: insert, ui: define, ui: include, and ui: composition.
- Param tags are for passing parameters to the template file.
- Custom tags to render custom contents.
- Remove tag used to stop rendering commented out JSF code on client-side.
Q #23) Describe JSF Standard tag libraries
Answer: JSF standard tag library consists of various HTML input types. Some of them are as listed below:
- h:inputText: Creates HTML text box of text type
- h:inputSecret: HTML text box of password type
- h:inputTextarea: HTML textarea input field
- h:inputHidden: HTML input of type hidden
- h:selectBooleanCheckbox: For single HTML check box
- h:selectManyCheckbox: For group of HTML check boxes
- h:selectOneRadio: For single HTML radio button
- h:selectOneListBox: For HTML single list box
Q #24) Discuss the importance of @ManagedProperty annotation in JSF 2.0.
Answer: Using @ManagedProperty annotation, the bean can be injected into another bean property by using the bean’s setter method. We need a value attribute containing the bean name that is required to be injected.
Q #25) What does @ApplicationScoped annotation indicate?
Answer: @ApplicationScoped annotation is used to define the scope within which bean will be stored and remains across user interaction within web applications. Managed beans are usually lagging to instantiate.
In order to instantiate application scoped beans with the start of the application. Eager attribute is made true in managed bean.
Q #26) List data bound table components from JSF 2.0.
Answer: <h:dataTable> tag is used to create data table and data bound table components in JSF consists of elements like Column Sorting (Ascending/Descending), Checkboxes, Pagination.
Q #27) List different implementation of JSF.
Answer: Implementations are listed below:
|Apache MyFaces||Oracle Mojarra||OmniFaces|
Q #28) Explain immediate and rendered attributes in JSF.
Answer: When the Boolean value of an immediate attribute is set as true, it will force conversions, events, and validation processing during the request phase of the JSF lifecycle.
Rendered attribute would decide component should be rendered or not in view page, it uses arithmetic literals and operators with rvalue expression and not with lvalue expression.
Q #29) List benefits of JSF data table tags.
Answer: JSF data table tags are used to display relational data in tabular format on JSF view pages.
Benefits offered by Data Table tags are:
- In order to display data, the data table can iterate over arrays or collection of values.
- Data Table tags offer attributes to modify its data that is displayed on the JSF view page.
Q #30) Differentiate between JSF 1.0 and JSF 2.0.
Answer: Differences between the two are enlisted below:
|Managed beans are registered in faces-config.xml with JSF 1.2||Annotations are used to register managed beans in JSF 2.0|
|In JSF 1.0 Faces Servlet are configured in web.xml with url-pattern as /faces/*||In JSF 2.0 Faces Servlet are configured in web.xml with url-pattern as *.xhtml|
|JSF 1.2 was released with EL and improved and unified view handling in order to remove tight coupling with JSP.||JSF 2.0 offers GET requests support, new view scope, use of annotatios, generics, includes facelets as default view in place of JSP and resource handling|
|JSF 1.0 use API for custom components||JSP is deprecated in JSF2.0 and replaced by Facelets (xhtml)|
Q #31) Explain best practices applied to build JSF application.
Answer: Best practices or rules to be followed during web development using JSF related to various scope backing beans are as follows:
- Begin with @RequestScoped Bean until business data and state looses and then choose the next higher level scope.
- To stop sharing data or state among all users, avoid @ApplicationScoped scope.
- To stop sharing data or state among all browser tabs, avoid @SessionScope.
- To prevent losing data and state over HTTP requests, avoid @RequestScope.
- View, Flow, Session, and Conversation scoped beans have no effect on users.
- Avoid calling business logic in getter methods JSF backing beans.
- Use result set in a value property.
- @PostConstruct annotated method is used to compute result set.
Q #32) Describe the architecture of JavaServer Faces
Answer: JavaServer Faces implements MVC design pattern, components are separately defined, with different logic.
- View component for presentation logic, and
- Model component for business logic is implemented.
- Controller component controls navigation from one view to another and executes various model methods.
Controller part consists of
- Controller servlet i.e. FacesServlet
- Centralized configuration file i.e. faces-config.xml
- Set of event handlers for web applications.
Q #33) What are the differences between JSF and the Spring framework?
Answer: Differences between the two are listed below:
|Java Server Faces||Spring|
|JSF are used for creating component based user interfaces for web application.||Spring uses DispatcherServlet and model-view-controller design pattern for web application.|
|It has mature user interface framework comprehensive and rich request life cycle server side components||Spring offers application framework for standalone, web and distributed applications.|
|JSF allows integration with HTML, JSP in developing view component of model view controller.||Velocity, Free marker etc are used for view component with Spring framework|
|It does not have predefined support to integrate JDBC, EJBs, Hibernate and RMI||It offers predefined support for JDBC, EJB, Hibernate, RMI etc.|
|JSF is a heavy weight framework||Spring is a light weight framework|
|It has more API dependency and have no support for POJO implementations||It has POJO implementations and less API dependency.|
|JSF does not support middleware services like JAAS, JNDI, JMS and Java Mail||Supports middleware services like JAAS, JNDI, JMS and Java Mail|
JavaServer Faces is a Java web-based framework that designs user interface components using managed beans, backing beans, Facelets, and other custom libraries such as prime faces or ice faces.
Application architects, developers, component, and page authors should be well conversant with various frameworks such as Java Server Faces, besides other frameworks such as Spring, Hibernate, and Struts.
This JSF interview questions tutorial helps you prepare for most JSF interviews.