Being Stateful

I l @ ve RuBoard

Suppose that you'd like a bean that is a little less flighty than a stateless session bean. You can create a stateful bean instead. For example, extend your cart bean to carry a total of items that you've added to your cart and then compute the tax and the total.

To begin, create a new set of classes based on the name StatefulCart (shown in Listings 17.9, 17.10 and 17.11).

Listing 17.9 StatefulCart.java
 package com.bfg.ejb.cart; import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface StatefulCart extends EJBObject {    public void clearTotal() throws RemoteException;    public void addItem(double price, int quantity) throws RemoteException;    public double getTotal() throws RemoteException;    public double computeTax(String state)  throws RemoteException; } 
Listing 17.10 StatefulCartHome.java
 package com.bfg.ejb.cart; import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface StatefulCartHome extends EJBHome {     StatefulCart create() throws RemoteException, CreateException; } 
Listing 17.11 StatefulCartBean.java
 package com.bfg.ejb.cart; import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class StatefulCartBean implements SessionBean {     double total = 0D;     public void clearTotal() {       total = 0D;     }     public void addItem(double price, int quantity) { total += price * quantity;     }     public double getTotal() {      return total;     }     public double computeTax(String state)     {           if ((state != null) &&               (state.compareTo("MA") < 0)) {               return total * 0.05D;           }  else {                return total * 0.10D;           }     }    public void ejbCreate()    {}    public void ejbPostCreate()    {}    public void ejbRemove()    {}    public void ejbActivate()    {}    public void ejbPassivate()    {}    public void setSessionContext(SessionContext sc)    {} } 

As you can see, this has simply taken the old cart bean and added a state variable to hold the total; it also used new methods to get, add to, and clear the total. The compute tax method has changed to take only the state as an argument.

Now add the new bean to the ejb-jar.xml and jboss.xml files (see Listings 17.12 and 17.13).

Listing 17.12 Additions to ejb-jar.xml
 <session>   <ejb-name>StatefulCart</ejb-name>   <home>com.bfg.ejb.cart.StatefulCartHome</home>   <remote>com.bfg.ejb.cart.StatefulCart</remote>   <ejb-class>com.bfg.ejb.cart.StatefulCartBean</ejb-class>   <session-type>Stateful</session-type>   <transaction-type>Bean</transaction-type> </session> 
Listing 17.13 Additions to jboss.xml
 <session>   <ejb-name>StatefulCart</ejb-name>   <jndi-name>bfg/StatefulCart</jndi-name> </session> 

Finally, write some JSP to test it (see listing 17.14).

Listing 17.14 TestStatefulTax.jsp
 <%@ page import="javax.naming.*" %> <%@ page import="javax.naming.directory.*" %> <%@ page import="java.util.Hashtable" %> <%@ page import="com.bfg.ejb.cart.StatefulCartHome" %> <%@ page import="com.bfg.ejb.cart.StatefulCart" %> <%@ page import="javax.rmi.PortableRemoteObject" %> <% Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,      "org.jnp.interfaces.NamingContextFactory"); env.put(Context.PROVIDER_URL,      "jnp://localhost:1099"); env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); DirContext ctx = new InitialDirContext(env); Object ref = ctx.lookup("bfg/StatefulCart"); StatefulCartHome home =      (StatefulCartHome) PortableRemoteObject.narrow(ref, StatefulCartHome.class); StatefulCart cart = home.create(); cart.clearTotal(); cart.addItem(4.95D, 2); cart.addItem(2.00D, 4); %>     Total before tax is <%= cart.getTotal() %><BR>     Tax on .00 in MA is <%= cart.computeTax("MA") %><BR>     Tax on .00 in CA is <%= cart.computeTax("CA") %><BR> 

Because you're using a stateful bean, the total persists between the calls to addItem and you get the right result, as shown in Figure 17.2.

Figure 17.2. The stateful cart comes through.

graphics/17fig02.jpg

I l @ ve RuBoard


MySQL and JSP Web Applications. Data-Driven Programming Using Tomcat and MySQL
MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL
ISBN: 0672323095
EAN: 2147483647
Year: 2002
Pages: 203
Authors: James Turner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net