title: My first reverse ajax application
body: <p>I want to learn about reverse ajax, I found a gadget called ICEPush and I thought it could be a good starting point. I am having trouble implementing a very simple application. 
  I am following this <a href="http://wiki.icesoft.org/display/ICE/Easy+Ajax+Push#EasyAjaxPush-downloads" rel="nofollow">tutorial</a>, but instead of Tomcat, I am using Glassfish 3.1 and instead of Eclipse I am using NetBeans 7.1</p>
  
  <p>I did exactly as it says in the tutorial, see my code.
  This is the page that will be the target of the Ajax push:</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"&gt;
      &lt;h:head&gt;
          &lt;title&gt;Easy Ajax Push - Color&lt;/title&gt;
      &lt;/h:head&gt;
  
          &lt;h:body&gt;
              &lt;h:dataTable value="#{messageBean.textList}" var="current"&gt;
                  &lt;h:column&gt;
                      &lt;h:outputText value="#{current.text}"
                                    style="color: #{current.color};"/&gt;
                  &lt;/h:column&gt;
              &lt;/h:dataTable&gt;
  
              &lt;hr width="100%"/&gt;
  
              &lt;h:form&gt;
                  &lt;h:panelGrid columns="4"&gt;
                      Choose a Color:
                      &lt;h:commandButton value="Red"
                                       action="#{colorBean.chooseColor}"
                                       style="color: white; background-color: red;"&gt;
                          &lt;f:setPropertyActionListener target="#{colorBean.color}" value="red"/&gt;
                      &lt;/h:commandButton&gt;
                      &lt;h:commandButton value="Blue"
                                       action="#{colorBean.chooseColor}"
                                       style="color: white; background-color: blue;"&gt;
                          &lt;f:setPropertyActionListener target="#{colorBean.color}" value="blue"/&gt;
                      &lt;/h:commandButton&gt;
                      &lt;h:commandButton value="Green"
                                       action="#{colorBean.chooseColor}"
                                       style="color: white; background-color: green;"&gt;
                          &lt;f:setPropertyActionListener target="#{colorBean.color}" value="green"/&gt;
                      &lt;/h:commandButton&gt;
                  &lt;/h:panelGrid&gt;
              &lt;/h:form&gt;
  
          &lt;/h:body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Here are the 2 managed beans that are needed:
  <strong>ColorBean.java</strong></p>
  
  <pre><code>@ManagedBean(name="colorBean")
  @ViewScoped
  public class ColorBean implements  Serializable {
  
     private static final String PUSH_GROUP = "colorPage";
  
      @ManagedProperty(value="#{messageBean}")
      private MessageBean messageBean;
      private String color = "black";
      private String sessionId;
  
      public ColorBean() {            
                  PushRenderer.addCurrentSession(PUSH_GROUP);
          FacesContext fcontext = FacesContext.getCurrentInstance();
          HttpSession session = (HttpSession)fcontext.getExternalContext().getSession(false);
          sessionId = session.getId();
      }
  
      public void setMessageBean(MessageBean messageBean) {
          this.messageBean = messageBean;
      }
  
      public String getColor() {
          return color;
      }
  
      public void setColor(String color) {
          this.color = color;
      }
  
      public String chooseColor() {
          messageBean.addToList(sessionId, color);
                  PushRenderer.render(PUSH_GROUP);
          return null;
      }
  }
  </code></pre>
  
  <p><strong>MessageBean.java</strong></p>
  
  <pre><code>@ManagedBean(name="messageBean")
  @ApplicationScoped
  public class MessageBean implements  Serializable {
  
      private static final int MAX_SIZE = 25;
      private List&lt;TextModel&gt; textList = new ArrayList&lt;TextModel&gt;(0);
  
      public MessageBean() {
      }
  
      public List&lt;TextModel&gt; getTextList() {
          return textList;
      }
  
      public void setTextList(List&lt;TextModel&gt; textList) {
          this.textList = textList;
      }
  
      public void addToList(String sessionId, String color) {
          textList.add(makeTextModel(sessionId, color));
  
          if (textList.size() &gt; MAX_SIZE) {
              textList.clear();
          }
      }
  
      private TextModel makeTextModel(String sessionId, String color) {
          return new TextModel("User with session ID of " + sessionId + " selected color \"" + color + "\".",
                               color);
      }
  }
  </code></pre>
  
  <p>Also there is a simple pojo to represent the text being presented.
  <strong>TextModel.java</strong></p>
  
  <pre><code>public class TextModel implements Serializable {
  
      private String text;
      private String color;
  
      public TextModel() {
      }
  
      public TextModel(String text, String color) {
          this.text = text;
          this.color = color;
      }
  
      public String getText() {
          return text;
      }
  
      public void setText(String text) {
          this.text = text;
      }
  
      public String getColor() {
          return color;
      }
  
      public void setColor(String color) {
          this.color = color;
      }
  
      public String toString() {
          return text;
      }
  }
  </code></pre>
  
  <p>I am using IceFaces version 3.0.1 and this is how my <strong>web.xml</strong> looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.PROJECT_STAGE&lt;/param-name&gt;
          &lt;param-value&gt;Development&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.STATE_SAVING_METHOD&lt;/param-name&gt;
          &lt;param-value&gt;server&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.FACELETS_SKIP_COMMENTS&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;org.icefaces.mandatoryResourceConfiguration&lt;/param-name&gt;
          &lt;param-value/&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;org.icefaces.ace.theme&lt;/param-name&gt;
          &lt;param-value&gt;sam&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.VALIDATE_EMPTY_FIELDS&lt;/param-name&gt;
          &lt;param-value&gt;false&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;com.icesoft.faces.gmapKey&lt;/param-name&gt;
          &lt;param-value&gt;ABQIAAAADlu0ZiSTam64EKaCQr9eTRTOTuQNzJNXRlYRLknj4cQ89tFfpxTEqxQnVWL4k55OPICgF5_SOZE06A&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;Resource Servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;com.icesoft.faces.webapp.CompatResourceServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.jsf&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/icefaces/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Resource Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/xmlhttp/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;session-config&gt;
          &lt;session-timeout&gt;
              30
          &lt;/session-timeout&gt;
      &lt;/session-config&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;faces/index.xhtml&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>This code gives me 3 problems:</p>
  
  <p>1 - When i run the app and i click in some of the 3 buttons,get an exception that says that i cannot call one use one managed bean inside the other, because their scopes are not compatible:</p>
  
  <blockquote>
    <p>WARNING: queued exception javax.faces.FacesException: Unable to create
    managed bean colorBean.  The following problems were found:
         - The scope of the object referenced by expression #{messageBean}, request, is shorter than the referring managed beans (colorBean) scope of view</p>
  </blockquote>
  
  <p>2 - In the console all the time i see a message that says:</p>
  
  <blockquote>
    <p>WARNING: PWC4011: Unable to set request character encoding to UTF-8
    from context /ReverseAjaxExample, because request parameters have
    already been read, or ServletRequest.getReader() has already been
    called</p>
  </blockquote>
  
  <p>3 - If i change the @ApplicationScope to @ViewScope and the @ViewScope to @ApplicationScope in the managed beans, the first problem disappears and I can see how the application works, but the reverse ajax does not work, because other browsers do not display the changes. And also I always keep seeing the warning <code>PWC4011</code> in the console</p>
  
  <p>I never worked with reverse Ajax but I understand it from the theoretical point of view. I'll really appreciate it if you could give me a hand fixing this simple app.</p>