What s Next


Using Stateless Session Beans

Resin-EE provides support for instantiating stateless session beans in addition to entity beans. A stateless session EJB is basically an object that can be instantiated both locally or remotely to handle business-related issues within the application. The stateless session is like a servlet except it doesn't have the ability to handle state. The sessions are useful for the division of tasks within a servlet.

With the LoginMaintenanceServlet you created earlier, as you added each task to the servlet, more and more code was required. You can delegate each of the individual tasks to a stateless session EJB. Creating a stateless session bean is similar to creating an entity bean. Four steps are required:

  • Create a remote interface.

  • Create a local home interface.

  • Create an implementation file.

  • Create a deployment descriptor.

Since the stateless session can be instantiated remotely, you need a remote interface:

 package entitlements; import java.rmi.*; import javax.ejb.*; public interface LoginUpdate extends EJBObject {   String getLogin(int e) throws RemoteException; } 

The only major difference here from your local interface is that the getLogin() method throws a RemoteException. Now the local interface:

 package entitlements; import java.rmi.*; import javax.ejb.*; public interface LoginUpdateHome extends EJBHome {   LoginUpdate create() throws CreateException, RemoteException; } 

The local home interface has a single purpose: to create the remote object defined by the remote interface above. Now for the implementation code:

 package entitlements; import java.rmi.*; import javax.ejb.*; import com.caucho.ejb.*; public class LoginUpdateBean extends AbstractSessionBean {   private LoginHome home = null;   public void ejbCreate() {     throws ServletException {     try {       Context cmp = (Context) new InitialContext().lookup("java:comp/env/cmp");       home = (LoginHome) cmp.lookup("Login");     } catch (NamingException e) {       throw new ServletException(e);     }   }   public String getLogin(int e){     Login login = (Login)home.findByPrimaryKey(e);     return login.getLogin();   } } 

The code for the stateless session has two methods. The first is the ejbCreate() method, which is called when the session bean is first referenced. The method finds the local interface to the Login entity bean and stores it for later use. When a call is made to the getLogin(int) method, the bean accesses the home interface, calls the findByPrimaryKey() method to get a Login entity bean based on the passed key value, and returns the login string from the row.

You need to make sure the server knows about the bean by including an entry in the deployment descriptor:

 <ejb-jar> <enterprise-beans>   <session>     <ejb-name>LoginUpdate</ejb-name>     <home>entitlements.LoginUpdateHome</home>     <remote>entitlements.LoginUpdate</remote>     <ejb-class>entitlements.LoginUpdateBean</ejb-class>     <session-type>Stateless</session-type>     <transaction-type>Bean</transaction-type>   </session> </enterprise-beans> </ejb-jar> 

Of course, you need to be able to remotely access the session bean. To do this, you will write a JSP page:

 <%@ page import="com.caucho.burlap.*, entitlements.*, javax.ejb.*" %> <% HomeHandle homeHandle = new BurlapHomeHandle(   "http://localhost:8080/entitlements/LoginUpdate"); // get the home stub LoginUpdateHome home = (LoginUpdateHome) homeHandle.getEJBHome(); // create remote object LoginUpdate login = (LoginUpdate) home.create(); %> Login name for row 1: <%= login.getLogin(1) %> 

As you can see, the code isn't very complex. First you attempt to make a connection to the remote server using Burlap. The URL is the same URL you might use in a browser. The outcome of the BurlapHomeHandle is a handle to the remote system. You use this handle to obtain the home interface of your session bean by calling homeHandle.getEJBHome(). A LoginUpdateHome object is returned. Using the home interface, you call the create() method to create the remote LoginUpdate bean and obtain a local object pointer or reference to the bean. Finally, you make a call to the getLogin(int) method to obtain the login name of the associated table row.




Mastering Resin
Mastering Resin
ISBN: 0471431036
EAN: 2147483647
Year: 2002
Pages: 180

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