Browse DevX
Sign up for e-mail newsletters from DevX


How Java EE 6 Scopes Affect User Interactions : Page 3

Java EE 6 supports a suite of five scopes, each with its own behavior for managing the user's interaction with a Java Web application. Find out how and when to use them.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Java EE 6 Conversation Scope

Conversation scope is dedicated to the user's interaction with JSF applications and represents a unit of work from the point of view of the user. Think of conversation scope as a developer-controlled session scope across multiple invocations of the JSF lifecycle. The developer can explicitly set the conversation scope boundaries and can start, stop or propagate the conversation scope based on business logic flow. All long-running conversations are scoped to a particular HTTP Servlet session and may not cross session boundaries. In addition, conversation scope holds the state associated with a particular Web browser tab in a JSF application.

Working with conversation scope is a little bit different from the rest of the scopes. First, you mark the Bean with @ConversationScope, represented by the javax.enterprise.context.ConversationScoped class. Second, CDI provides a built-in Bean (javax.enterprise.context.Conversation) for controlling the lifecycle of conversations in a JSF application. This Bean may be obtained by injection, like this:

private @Inject Conversation conversation;

Now, the current request should be transformed into a long-running conversation by calling the begin() method. You also need to prepare for the destruction of the conversation by calling the end() method.

In the following example, a conversation-scoped Bean controls the conversation with which it is associated. Our Bean will store the generated random numbers for only the length of the conversation:

package rnd.beans; import java.io.Serializable; import java.util.ArrayList; import java.util.Random; import javax.enterprise.context.Conversation; import javax.inject.Named; import javax.enterprise.context.ConversationScoped; import javax.inject.Inject; @Named(value="rndBeanConversation") @ConversationScoped public class rndBeanConversation implements Serializable{ private @Inject Conversation conversation; /** Creates a new instance of rndBeanConversation */ public rndBeanConversation() { } private ArrayList rnds = new ArrayList(); private int rnd = 0; public int getRnd() { return rnd; } public void setRnd(int rnd) { this.rnd = rnd; } public ArrayList getRnds() { return rnds; } public void setRnds(ArrayList rnds) { this.rnds = rnds; } public void newRnd() { this.rnd = new Random().nextInt(100); this.rnds.add(rnd); } public void startRnd(){ conversation.begin(); } public void stopRnd(){ conversation.end(); } }

Moreover, the user can control when to start/stop the conversation through a set of JSF buttons:

Just generated: <h:outputText value="#{rndBeanConversation.rnd}"/>

List of generated numbers: <h:dataTable var="t" value="#{rndBeanConversation.rnds}"> <h:column> <h:outputText value="#{t}"/> </h:column> </h:dataTable> <h:form> <h:commandButton value="Start Conversation" actionListener="#{rndBeanConversation.startRnd}" action="index.xhtml"/> <h:commandButton value="Get Random" actionListener="#{rndBeanConversation.newRnd}" action="index.xhtml"/> <h:commandButton value="Stop Conversation" actionListener="#{rndBeanConversation.stopRnd}" action="index.xhtml"/> </h:form>

In this example, the conversation context automatically propagates with any JSF faces request or redirection (this facilitates the implementation of the common POST-then-redirect pattern), but it does not automatically propagate with non-faces requests such as links. In this case, you need to include the unique identifier of the conversation as a request parameter. The CDI specification reserves the request parameter cid for this use. The below example will propagate the conversation context over a link:

<h:link outcome="/link.xhtml" value="Conversation Propagation"> <f:param name="cid" value="#{conversation.id}"/> </h:link>

Java EE 6 Dependent Scope

This is the default scope when none is specified. In this case, an object exists to serve exactly one Bean and has the same lifecycle as that Bean. It also can be explicitly specified by annotating the Bean with the @Dependent annotation and import javax.enterprise.context.Dependent. The Bean below shows this:

package rnd.beans; import java.util.ArrayList; import java.util.Random; import javax.enterprise.context.Dependent; import javax.inject.Named; @Named(value = "rndBeanDependent") @Dependent public class rndBeanDependent { private ArrayList rnds = new ArrayList(); private int rnd = 0; /** Creates a new instance of rndBeanDependent */ public rndBeanDependent() { } public int getRnd() { return rnd; } public void setRnd(int rnd) { this.rnd = rnd; } public ArrayList getRnds() { return rnds; } public void setRnds(ArrayList rnds) { this.rnds = rnds; } public void newRnd() { this.rnd = new Random().nextInt(100); this.rnds.add(rnd); } }

The JSF code for testing the above Bean is:

Just generated: <h:outputText value="#{rndBeanDependent.rnd}"/>

List of generated numbers: <h:dataTable var="t" value="#{rndBeanDependent.rnds}"> <h:column> <h:outputText value="#{t}"/> </h:column> </h:dataTable> <h:form> <h:commandButton value="Get Random" actionListener="#{rndBeanDependent.newRnd}" action="index.xhtml"/> </h:form>

Comparing Java EE 6 Scopes

Grouping the above Beans into a single application will allow you to test and see the differences between the Java EE 6 scopes. Figure 1 below is a screenshot test that reveals each scope's advantages and disadvantages.

Click here for larger image

Figure 1. Java EE 6 Scopes in a Single Application

As you can see, the dependent and request scopes do not store Bean state over multiple HTTP requests, while the session, application and conversation scopes store Bean state at different levels over multiple HTTP requests.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date