Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

How Java EE 6 Scopes Affect User Interactions : Page 2

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.


advertisement

Java EE 6 Session Scope

Request scope is very useful in any Web application when you need a single interaction per HTTP request. However, when you want a user's interaction with a Web application across multiple HTTP requests, that's when the session scope goes into action. Session scope allows you to create and bind objects to a session. For example, you can add the session scope to your random generator for storing a list of generated numbers across multiple requests.

In Java EE 6, you place a Java Bean under session scope by annotating the Bean with @SessionScoped and importing the javax.enterprise.context.SessionScoped class. For example, this Bean generates random numbers and stores them into an ArrayList:



package rnd.beans; import java.io.Serializable; import java.util.ArrayList; import java.util.Random; import javax.inject.Named; import javax.enterprise.context.SessionScoped; @Named(value = "rndBeanSession") @SessionScoped public class rndBeanSession implements Serializable{ /** Creates a new instance of rndBeanSession */ public rndBeanSession() { } 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); } }

For example, you can build a simple test for the above Java Bean using JSF:

Just generated: <h:outputText value="#{rndBeanSession.injrnd}"/>

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

Now, when you press the Get Random button multiple times, each generated number is stored into the rnds list. This time, the list stores those numbers for real, which is proving the effect of session scope. The output shows you a list of numbers representing the user's interaction with the Web application across multiple HTTP requests.

Java EE 6 Application Scope

An application scope extends session scope with shared state across all users' interactions with a Web application. More precisely, objects settled on application scope can be accessed from any page that is part of the application (e.g. JSF, JSP, XHTML, etc.). Usually, application scope objects are used as counters. For example, application scope can be used to count how many users are online or to share that information with all users.

In Java EE 6, you place a Java Bean under the application scope by annotating the Bean with @ApplicationScoped and importing the javax.enterprise.context.ApplicationScoped class. For example, this Bean generates random numbers and stores them in an ArrayList shared with all users by using the application scope.

package rnd.beans; import java.util.ArrayList; import java.util.Random; import javax.inject.Named; import javax.enterprise.context.ApplicationScoped; @Named(value="rndBeanApplication") @ApplicationScoped public class rndBeanApplication { /** Creates a new instance of rndBeanApplication */ public rndBeanApplication() { } 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); } }

Testing this Bean with multiple machines will reveal the same list of random numbers. Here is a possible JSF code for this:

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

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



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap