In this tutorial, we will show you how to develop and deploy a JSF 2.0 web application in Google App Engine (GAE) environment.
Tools and technologies used :
  1. JDK 1.6
  2. Eclipse 3.7 + Google Plugin for Eclipse
  3. Google App Engine Java SDK 1.6.3.1
  4. JSF 2.1.7
Note
This example is going to reuse this JSF 2.0 hello world example, modify it and merge it with this GAE + Java example.

1. New Web Application Project

In Eclipse, create a new Web Application project, named as “JSFGoogleAppEngine“.
generate a new web application GAE project
“Google Plugin for Eclipse” will generate a sample of GAE project structure.

2. JSF 2 Dependencies

To use JSF 2 in GAE, you need following jars
  1. jsf-api-2.1.7.jar
  2. jsf-impl-2.1.7.jar
  3. el-ri-1.0.jar
Copy and put it in “war/WEB-INF/lib” folder.
gae jsf2 dependency libraries
Right click on project folder, select “Properties“. Select “Java Build Path” -> “Libraries” tab, click “Add Jars” button and select above jars.
gae jsf2 java build path
Note
You need to put this el-ri-1.0.jar, otherwise, you will hit error message – Unable to instantiate ExpressionFactory ‘com.sun.el.ExpressionFactoryImpl’.

3. JSF Managed bean

3.1 Delete plugin generated JSFGoogleAppEngineServlet.java, you don’t need this.
3.2 Create a managed bean.
File : src/com/mkyong/HelloBean.java
package com.mkyong;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
import java.io.Serializable;
 
@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {
 
 private static final long serialVersionUID = 1L;
 
 private String name;
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
}
3.3 Create a new WebConfiguration.java.
JSF 2 is using “javax.naming.InitialContext” that’s not support in GAE.
To solve this, you need to get a copy of the JSF’s source code, clone the WebConfiguration.java, comment methods that are using “javax.naming.InitialContext” class, put it in “src/com/sun/faces/comfig/WebConfiguration.java“. Now, your newly created WebConfiguration.java class will overload the original WebConfiguration.java.
I don’t think GAE team will white-list this jar, just hope JSF’s team can fix this in future release.

4. JSF Pages

4.1 Create hello.xhtml page, accept a user input and pass it to helloBean.
File : war/hello.xhtml
version="1.0" encoding="UTF-8"?>