Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
EJB
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Building a Simple EJB Application –A Tutorial

                         

By Jeevaraj Gnanaselvan Dhanaraj (jeevaraj_1970@yahoo.com)
(Jeeva has over 7 years of experience in designing and developing enterprise class web applications using JAVA and J2EE technologies. He currently works for Itreya Technologies, Bangalore, leading a team of over 10 programmers and designers, developing a multi-user, distributed, web-based workflow application)

 Introduction

In this tutorial we will create a simple session EJB and a client web application using eclipse IDE along with Lomboz plug in and XDoclet. This application,    while simple, provides a good introduction to EJB development and some of the Web development tools available.

·         Environment

J2SDK 1.4.2
http://java.sun.com/

Eclipse 3.1

http://www.eclipse.org/

JBoss 4.0.2

http://www.jboss.org/  

XDoclet 1.2.3
http://xdoclet.sourceforge.net

Lomboz 3.1RC2
http://lomboz.objectweb.org/

·         Installation

Install JDK (in D:\j2sdk1.4.2_04)

Install JBoss (in E:\jboss-4.0.2)

Install Xdoclet (in D:\xdoclet\xdoclet-1.2.3)

Install Eclipse (in E:\Eclipse3.1)

Install Lomboz (in E:\Eclipse3.1) 

·         Setting up

  1. Set up the installed JRE in eclipse (Windows -> Preferences -> Java -> Installed JREs)

 

           

  1. Set up the installed runtime for server in eclipse (Windows -> Preferences -> Server -> Installed Runtimes)

 

 

 

  1. Set up Xdoclet in eclipse (Windows -> Preferences -> J2EE Annotations -> XDoclet)

  1. Set up the ejbdoclet for JBoss in eclipse (Windows -> Preferences -> J2EE Annotations -> Xdoclet -> ejbdoclet)

 

·         Creating a Session Bean

  1. Open the J2EE perspective in eclipse (Windows -> Open Perspective -> Other -> J2EE)

   

 

 

 

  1. Create a new EJB Project from the Project Explorer (EJB Projects -> New -> EJB Project)

 

  1. Enter name as “SimpleEJBTutorial” and select project location as “E:\Test”.

 

 

  1. Now create a new Session Bean from the Project Explorer (EJB Projects -> Simple EJB Tutorial -> ejbmodule -> New -> Other…)

 

 

 

 

 

 

 

 

 

 

   

 

 

Note:

XDoclet is an extended Javadoc Doclet engine. It's a generic Java template engine that lets us create custom Javadoc @tags and based on those @tags generate source code or other files (such as deployment descriptors in xml form). XDoclet supports a set of common standard tasks such as web.xml or ejb-jar.xml generation. It uses special JavaDoc @tags to define settings for each component. For example, putting a @ejb.bean name="Hello" jndi-name="Hello" type="Stateless" in HelloBean.java

We have to edit only the HelloBean.java. All others are automatically generated and XDoclet will regenerate them each time we make a change to the HelloBean class.

5.       Edit the HelloBean class and change the foo method to

             public String sayHello(String param) {

                        return "Hello..";

            }

The XDoclet builder will start working again and update our classes. At the end our projects will look like: 

 

 

The highlighted classes HelloBean.java and HelloSession.java are server side classes and Hello.java and HelloHome.java are public interfaces. These are the classes that will be needed by all clients.  

·         Deploying the Session Bean 

  1. Open the Servers view (Windows -> Show View -> Servers)

 

  1. Create a new server by right-clicking inside the “Servers” view.

 

 

 

 

 

 

  1. Choose SimpleEJBTutorial from the available projects.

Ensure that the option “Automatically publish when starting server” is checked.

 

 

 

  1. Look for console messages such as:

22:02:13,274 INFO  [EjbModule] Deploying Hello

22:02:13,556 INFO  [EJBDeployer] Deployed: file:/E:/jboss-4.0.2/server/default/deploy/SimpleEJBTutorial.jar

 

 

Now the EJB has been defined and deployed. It is ready to be used. We can now build a client to make the HelloBean say Hello.

·         Creating a Web client Application

  1. Create a new Dynamic Web Project namely “HelloWeb” from the Project Explorer (Dynamic Web Projects -> New -> Dynamic Web Project)

 

 

We will need to access the EJB interface types such as Hello and HelloHome in client applications.

These classes are required to be added in the build path.  

  1. In the Project Explorer, right click on the HelloWeb dynamic web project and Choose Properties...

 

  1. In the Java buildpath, add the SimpleEJBTutorial project to the project references. This will help allows us to compile against the latest ejb client classes in this web project.

 

 

  1. Create a new “test.jsp” under the HelloWeb\WebContent  directory.       

 

 

 

   

 

 

  1. Open test.jsp in the JSP source page editor.
  2. Add the following lines in the “body” of test.jsp

 <%

   com.tutorial.Hello hello = null;

   try{

         com.tutorial.HelloHome home = com.tutorial.HelloUtil.getHome();

         hello = home.create();

   }catch(Exception exception)

   {

   }

%>

<b><%= hello.sayHello(" my friend !") %></b>

 

 

  1. Save the test.jsp. Our web application is now complete.

·         Deploying and running the Web client Application

  1. Right click on test.jsp and select Run As -> Run on Server...

 

  1. Look for something like: [TomcatDeployer] deploy, ctxPath=/HelloWeb, warUrl=file:/E:/jboss-4.0.2/server/default/tmp/deploy/tmp52097HelloWeb-exp.war/ on the console.

 

 

  1. Same application can be invoked from browser using the url, http://localhost:8080/HelloWeb/test.jsp

 

 

 

  1. Experiment adding more methods and ejbs.

·         Conclusion

In this tutorial we learned how to configure Eclipse to work with XDoclet and create a J2EE EJB project that has a Session Bean. We created a client Web application. This application, while simple, provides a good introduction to EJB development and some of the Web development tools available.

 

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (post your own) View All Comments Latest 10 Comments:

hi all

this material is really helpful
thanks to author

Posted by Reddy on Thursday, 06.5.08 @ 12:58pm | #62170

Hi,
The tutorial is really nice and giver complete information on getting started with EJB2.0
Its really good for a beginner.
I really appriciate.

Pankaj Dubey
Software Engineer
CGI Inc.
Canada.

Posted by Pankaj Dubey on Wednesday, 05.7.08 @ 09:35am | #58772

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.