1
votes

I wanted to test Session replication of session and application scoped beans of JSF 2 in a clustered environment, currently trying with glassfish. To do so I created a simple application that I deployed to the server first to verify it's working (it doesn't).

Here is what I did so far:

  1. Setup a Ubuntu Virtual Machine in Oracle VirtualBox
  2. download glassfish as zip-file and unzip it in the home directory
  3. start glassfish
  4. deploy the JSF-app as war-file
  5. go to the page of the app: localhost:8080/jsftest/index.jsf
  6. try to set a variable saved in session/application scope

Depending on javax.faces.STATE_SAVING_METHOD in web.xml the result:

  • server: viewExpiredException is thrown
  • client: value is not stored and default value is shown

To create the app I did the following:

  1. create new dynamic web project in eclipse
  2. unzip all files from myfaces core in WEB-INF/lib
  3. set the classes-folder to WEB-INF/classes
  4. write beans and a .xhtml as below

The session scoped bean (application scoped is similiar):

@ManagedBean(name = "sessbean")
@SessionScoped
public class MySessionScopeBean implements Serializable{
private static final long serialVersionUID = -4733271400646173098L;
private String value = "default session data";

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

}

The index.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:f="http://java.sun.com/jsf/core"      
  xmlns:h="http://java.sun.com/jsf/html">

<h:head>
    <title>JSF 2.0 Hello World</title>
</h:head>
<h:body>
    <h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
    <h:outputText value="Session id: #{sessionScope['id']}" />
    <h:form>
        <h:panelGroup>
            <h:outputText value="Session Variable:"/>
            <br/>
            <h:inputText value="#{sessbean.value}"/>
        </h:panelGroup>
        <h:commandButton value="submit"/>
    </h:form>
    <h:form>
        <h:panelGroup>
            <h:outputText value="Application Variable:"/>
            <br/>
            <h:inputText value="#{appbean.value}"/>
        </h:panelGroup>
        <h:commandButton value="submit"/>
    </h:form>
</h:body>
</html>

So this very basic example doesn't work. Any suggestions?

Also the Session-ID is not shown by #{sessionScope['id']}, don't know if that is the way to do it.

1

1 Answers

0
votes

Got it working by removing parts of the (generated) web.xml file. The working file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>jsftest</display-name>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
</web-app>

What I stripped out (don't know which part caused the failure):

  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <description>
    This parameter tells MyFaces if javascript code should be allowed in
    the rendered HTML output.
    If javascript is allowed, command_link anchors will have javascript code
    that submits the corresponding form.
    If javascript is not allowed, the state saving info and nested parameters
    will be added as url parameters.
    Default is 'true'</description>
    <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <description>
    If true, rendered HTML code will be formatted, so that it is 'human-readable'
    i.e. additional line separators and whitespace will be written, that do not
    influence the HTML code.
    Default is 'true'</description>
    <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
    <param-value>false</param-value>
  </context-param>
  <context-param>
    <description>
    If true, a javascript function will be rendered that is able to restore the
    former vertical scroll on every request. Convenient feature if you have pages
    with long lists and you do not want the browser page to always jump to the top
    if you trigger a link or button action that stays on the same page.
    Default is 'false'
</description>
    <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
    <param-value>true</param-value>
  </context-param>
  <listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
  </listener>