Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using JSF 2 with Google App Engine

DZone's Guide to

Using JSF 2 with Google App Engine

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Since last week I have been playing with Google App Engine (GAE) and some of the frameworks that we normally use in enterprise applications. After developing some basic servlets to get comfortable with appspot I decided to use JSF 2. I wanted to see how well it fits in GAE aware of the fact that there are quite a few caveats mentioned on GAE website regarding JSF. I followed the obvious steps mentioned on the help pages but I think they are a bit outdated.

I decided to compile a list that one can follow to successfully get JSF 2.0.1 (Mojarra) working on GAE 1.2.6. If you have followed steps provided elsewhere and are still getting the following errors beware that you might have missed one or more of the setting provided.

Resources

  1. Will it play in GAE - Gives of list of technologies which are supported on GAE
  2. Configuring JSF 2 in GAE - Configuration provided by Google but this doesn't work straightaway
  3. JSF 2 Compatibility Issues - Compatibility issues with JSF 2
  4. JSF 2 impl jar fix - Josh has kindly provided a fix for JSF impl jar which resolves the startup problem

Setup steps:

  • Follow the steps provided in the jsf configuration guide on GAE website (see Resources:2)
  • Replace the jsf-impl.jar with provided on Josh's blog page . Stack trace fixed

 

WARNING: Error starting handlers Throwable occurred:
java.lang.NoClassDefFoundError:javax.naming.InitialContext

 

  • Now you app should start up on dev box but it when you deploy it on GAE you will get the following stack trace (lower the level to debug or warning)

 

Nested in javax.servlet.ServletException: java.lang.RuntimeException: java.lang.ClassNotFoundException: void:
java.lang.RuntimeException: java.lang.ClassNotFoundException: void
 at com.google.apphosting.runtime.jetty.SessionManager.deserialize(SessionManager.java:389)
 at com.google.apphosting.runtime.jetty.SessionManager.loadSession(SessionManager.java:307)
 at com.google.apphosting.runtime.jetty.SessionManager.getSession(SessionManager.java:282)
 at org.mortbay.jetty.servlet.AbstractSessionManager.getHttpSession(AbstractSessionManager.java:237)
 at org.mortbay.jetty.Request.getSession(Request.java:998)
 at com.sun.faces.application.WebappLifecycleListener.syncSessionScopedBeans(WebappLifecycleListener.java:393)

 

  • In order to fix the above exception make sure following configuration are in web.xml (These are recommendations from GAE JSF2 compatibility issues: see Resources:3)

 

<!-- Seems like GAE 1.2.6 cannot handle server side session management. At least for JSF 2.0.1 -->
  <context-param>
     <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
     <param-value>client</param-value>
  </context-param>
<!-- Recommendation from GAE pages  -->
  <context-param>
     <param-name>javax.faces.PROJECT_STAGE</param-name>
     <param-value>Production</param-value>
  </context-param>

 

  • Finally, I have noticed that sometimes objects kept in session loose data. For instance, I created a session scoped managed bean called CustomerBean and added customer name and email to it. When I move to second view, the name and email address stored in the managed bean are lost. Now this happens only on appspot not on the dev server so I can't debug and I haven't yet figured out why it this happens. I am working on a strange hack which put a token key value in the session in every request which solves the issue but I am sure why.
    If anyone has any clue why please post a comment.

Hopefully, these setting will get you started on your first JSF 2 app on Google App Engine.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}