Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article
Stardeveloper RSS Feed
Hosted by Securewebs.com
 
Home : J2EE : EJB : Building your first Enterprise JavaBean
 
RSS - Read full length articles at Stardeveloper using Stardeveloper RSS Feed RSS

First.java :
Now create a new First.java source file in com/stardeveloper/ejb/session folder. Copy and paste following code in it:

/* First.java */
package com.stardeveloper.ejb.session;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface First extends EJBObject {

    public String getTime() throws RemoteException;
}

Hit the 'save' button to save First.java source file.

Explanation :
First line is the package statement which tells that First interface belongs in the com.stardeveloper.ejb.session package :

package com.stardeveloper.ejb.session;

Next two lines are import statements for importing required classes.

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

Then comes interface declaration line which tells that this is an interface with name "First" which extends an existing interface javax.ejb.EJBObject.

Note: Every Remote interface *must* always extend EJBObject interface. It is a requirement, not an option.
public interface First extends EJBObject {

Now as I said on the last page, we have to declare methods in Remote interface which we want to be called by the client ( which the client can access and call ). For simplicity, we will only declare a single method, getTime(); which will return a String object containing current time.

public String getTime() throws RemoteException;

Notice that there are no {} parenthesis with this method as it has been declared in an interace. Remote interface method's must also throw RemoteException because EJBs are distributed components and during the call to an EJB, due to some network problem, exceptional events can arise, so all Remote interface method's must declare that they can throw RemoteException in Remote interfaces.

FirstHome.java :
Let's now create the Home interface for our FirstEJB. Home interface is used to create and get access to Remote interfaces. Create a new FirstHome.java source file in com/stardeveloper/ejb/session package. Copy and paste the following code in it :

/* FirstHome.java */
package com.stardeveloper.ejb.session;

import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;

public interface FirstHome extends EJBHome {

    public First create() throws CreateException, RemoteException;
}

Hit the 'save' button to save FirstHome.java source file.

Explanation :
First few lines are package and import statements. Next we declare our FirstHome interface which extends javax.ejb.EJBHome interface.

Note: All Home interfaces *must* extend EJBHome interface.
public interface FirstHome extends EJBHome {

We continue exploring FirstHome.java on the next page.

FirstHome.java :
Then we declare a single create() method which returns an instance of First Remote interface. Notice that all methods in Home interfaces as well must also declare that they can throw RemoteException. One other exception that they *must* declare that they can throw is CreateException.

public First create() throws CreateException, RemoteException;

We are done with creating Remote and Home interfaces for our FirstEJB. These two are the only things which our client will see, the client will remain absolutely blind as far as the actual implementation class, FirstEJB is concerned.

FirstEJB.java :
FirstEJB is going to be our main EJB class. Create a new FirstEJB.java source file in com/stardeveloper/ejb/session folder. Copy and paste following text in it :

/* FirstEJB.java */
package com.stardeveloper.ejb.session;

import javax.ejb.SessionBean;
import javax.ejb.EJBException;
import javax.ejb.SessionContext;
import java.rmi.RemoteException;
import java.util.Date;

public class FirstEJB implements SessionBean {

    public String getTime() {
        return "Time is : " + new Date().toString();
    }

    public void ejbCreate() {}
    public void ejbPassivate() {}
    public void ejbActivate() {}
    public void ejbRemove() {}
    public void setSessionContext(SessionContext context) {}
}

Hit the 'save' button to save FirstEJB.java source file.

Explanation :
First few lines are package and import statements. Next we declare our FirstEJB class and make it implement javax.ejb.SessionBean interface.

Note: All Session bean implementation classes *must* implement SessionBean interface.
public class FirstEJB implements SessionBean

Our first method is getTime() which had declared in our First Remote interface. We implement that method here in our FirstEJB class. It simply returns get date and time as you can see below :

    public String getTime() {
        return "Time is : " + new Date().toString();
    }

Then come 5 callback methods which are part of SessionBean interface and since we are implementing SessionBean interface, we have to provide empty implementations of these methods. Later in other articles when will build some really useful Session beans, we will then see some use of these callback methods there, for now we don't need them.

ejb-jar.xml :
Let's now create the EJB deployment descriptor file for our FirstEJB Session bean. Create a new ejb-jar.xml file in the FirstEJB/META-INF folder. Copy and paste following text in it :

<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems,
    Inc.//DTD Enterprise JavaBeans 2.0//EN"
    "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd">

<ejb-jar>
   <description></description>
   <enterprise-beans>
      <session>
         <display-name>FirstEJB</display-name>
         <ejb-name>First</ejb-name>
         <home>com.stardeveloper.ejb.session.FirstHome</home>
         <remote>com.stardeveloper.ejb.session.First</remote>
         <ejb-class>com.stardeveloper.ejb.session.FirstEJB</ejb-class>
         <session-type>Stateless</session-type>
         <transaction-type>Container</transaction-type>
      </session>
   </enterprise-beans>

   <assembly-descriptor>
      <container-transaction>
        <method>
            <ejb-name>First</ejb-name>
            <method-name>*</method-name>
        </method>
        <trans-attribute>Supports</trans-attribute>
      </container-transaction>

      <security-role>
         <description>Users</description>
         <role-name>users</role-name>
      </security-role>
   </assembly-descriptor>
</ejb-jar>

Previous ( 1 Gone )( 2 Remaining ) Next

Comments/Questions ( Threads: 32, Comments: 42 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Facing problem to Run EJB Sample
  2. How to make this?
  3. Best tutorial who want to start.
  4. Thanks very much very helpful
  5. Can I use Entity Bean with out session bean ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  6. Building your first Enterprise JavaBean
  7. Downloading EJB builder
  8. Exception while executing the above code
  9. how to debug in MyEclipse using JBoss server
  10. not able to deploy ejb,jsp in JBoss server
  11. HttpSession Replication
  12. How to Deploy the jar in Weblogic81?
  13. HTTP Status 404 - /firstEJB.jsp
  14. Regarding EJB tutorial
  15. ejb not bound ( 2 Replies )
  16. Ejb on Weblogic server using Eclipse ( 1 Reply )
  17. getting error while i am compiling FirstHome.java, how can i solve this error
  18. org.apache.jasper.JasperException: Unable to compile class for JSP
  19. Tomcat/Jboss: help me!! ( 1 Reply )
  20. Is it necessary to have EJBs deployed in EJBServer(JBoss) to be present in the client side
  21. not able to deploy jsp on tomcat server
  22. Not able to compile the java files.
  23. Great article. Thanks.
  24. Problem in EJB
  25. JBoss 3.2.1 and Tomcat 4.1.24 ( 2 Replies )
  26. JBoss 3.2.1 and Tomcat 4.1.18
  27. EJB on weblogic
  28. Thank you
  29. Return an object called client for example
  30. EJB Not Bound
  31. javax.servlet.ServletException: javax/net/SocketFactory ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  32. Tomcat/JBoss : Port Issue ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.