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)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : J2EE : EJB : Building your first Enterprise JavaBean
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed

ejb-jar.xml :
One or more EJBs are packaged inside a JAR ( .jar ) file. There should be only one ejb-jar.xml file in an EJB JAR file. So ejb-jar.xml contains deployment description for one or more than one EJBs. Now as we learned in an earlier article, there are 3 types of EJBs so ejb-jar.xml should be able to contain deployment description for all 3 types of EJBs.

Our ejb-jar.xml file for FirstEJB contains deployment description for the only EJB we have developed; FirstEJB. Since it is a Session bean, it's deployment description is contained inside <session></session> tags.

<ejb-jar>
   <description></description>
   <enterprise-beans>
      <session></session>
   </enterprise-beans>
</ejb-jar>

Now let's discuss different deployment descriptor tags inside the <session></session> tag. First is the <ejb-name> tag. The value of this tag should be name of EJB i.e. any name you think should point to your Session EJB. In our case it's value is "First". Then come <home>, <remote> and <ejb-class> tags which contain complete path to Home, Remote and EJB implementation classes. Then comes <session-type> tag whose value is either "Stateless" or "Stateful". In our case it is "Stateless" because our Session bean is stateless. For more info on Stateless and Stateful Session beans, please read "An Introduction to Enterprise JavaBeans" article. Last tag is <transaction-type>, whose value can be either "Container" or "Bean". We will learn more about transactions in another article, for now it is sufficient to say that the transactions for our FirstEJB will be managed by the container.

<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>
</ejb-jar>

Then there is a <container-transaction> tag which tells that all methods of FirstEJB "support" transactions. We will learn more about these tags and ejb-jar.xml as we continue to learn more about transactions and security in EJB environment in other articles. For now let's move forward.

Compiling the EJB Java source files :
Our directory and file structure till now looks something like following :

C:\Projects
    TomcatJBoss
    EJB
        FirstEJB
            src
                com
                    stardeveloper
                        ejb
                            session
                                First.java
                                FirstHome.java
                                FirstEJB.java
            META-INF
                ejb-jar.xml

You can compile all the Java source file by using a command like following on the command prompt :

C:\Projects\EJB\FirstEJB\src\com\stardeveloper\ejb\session>
	javac -verbose -classpath %CLASSPATH%;C:\JBoss\client\jboss-j2ee.jar
        -d C:\Projects\EJB\FirstEJB *.java
Note: The point to remember is to make sure jboss-j2ee.jar ( which contains J2EE package classes ) in the CLASSPATH, or you will get errors when trying to compile these classes.

If you have installed JBoss Server in a separate directory then substitute the path to jboss-j2ee.jar with the one present on your system. The point is to put jboss-j2ee.jar in the CLASSPATH for the javac, so that all EJB source files compile successfully.

On the next page we will learn how to package these .class files and .xml file into an easy to deploy JAR file.

Packaging EJB source files into a JAR file :
Till now our directory and file structure should look something like following :

C:\Projects
    TomcatJBoss
    EJB
        FirstEJB
            com
                stardeveloper
                    ejb
                        session
                            First.class
                            FirstHome.class
                            FirstEJB.class
            META-INF
                ejb-jar.xml
            src
                com
                    stardeveloper
                        ejb
                            session
                                First.java
                                FirstHome.java
                                FirstEJB.java

Now to package the class files and XML descriptor file together, run the following command at the command prompt :

C:\Projects\EJB\FirstEJB>jar cvfM FirstEJB.jar com META-INF

Running this command should produce an EJB JAR file with the name of FirstEJB.jar in the FirstEJB folder. But there is one thing still left to be done.

Adding JBoss specific configuration file :
Till now our FirstEJB.jar file contains generic EJB files and deployment description. To run it on JBoss we will have to add one other file into the META-INF folder of this JAR file, called jboss.xml. This file contains the JNDI mapping of FirstEJB.

So create a new jboss.xml file in the FirstEJB/META-INF folder where ejb-jar.xml file is present. Copy and paste the following text in it :

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS//EN"
	"http://www.jboss.org/j2ee/dtd/jboss.dtd">
<jboss>
	<enterprise-beans>
		<session>
			<ejb-name>First</ejb-name>
			<jndi-name>ejb/First</jndi-name>
		</session>
	</enterprise-beans>
</jboss>

To add this new jboss.xml file into our existing FirstEJB.jar file, run the following command at the DOS prompt :

C:\Projects\EJB\FirstEJB>jar uvfM FirstEJB.jar META-INF

Now our FirstEJB.jar is ready to be deployed to the JBoss Server.

iv. Deploying FirstEJB.jar on JBoss Server :
Deploying EJBs on JBoss is as easy as copying the FirstEJB.jar file and pasting it into the C:\JBoss\deploy folder. If JBoss is running, you should see text messages appearing on the console that this EJB is being deployed and finally deployed and started. As simple as that.

v. Creating the client JSP page :
Create new WEB-INF folder in C:\Projects\TomcatJBoss folder. Now create a new web.xml file and copy/paste following text in it :

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2.3.dtd">

<web-app>
</web-app>

As you can see, this web.xml is almost empty and is not doing anything useful. We still created it because Tomcat will throw an error if you try to access this /jboss context that we had created earlier without creating a /WEB-INF/web.xml file.


Previous ( 2 Gone )( 1 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.