adbrite adds

Your Ad Here

Java J2EE Best Answer

Read all the question and answer in the post and success the Interview.


search for Interview questions and job

Friday, November 14, 2008

Ejb session bean develpment guide

EJB Development Guide in J2EE Server
First you install j2ee server.
download from sun.com

Coding the Enterprise Bean
Every enterprise bean requires the following code:

Remote interface


Home interface


Enterprise bean class


Coding the Remote Interface
A remote interface defines the business methods that a client may call. The business methods are implemented in the enterprise bean code. The source code for the Converter remote interface follows.

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

public interface Converter extends EJBObject {

public double dollarToYen(double dollars) throws RemoteException;
public double yenToEuro(double yen) throws RemoteException;
}

Coding the Home Interface
A home interface defines the methods that allow a client to create, find, or remove an enterprise bean. The ConverterHome interface contains a single create method, which returns an object of the remote interface type. Here is the source code for the ConverterHome interface:

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

public interface ConverterHome extends EJBHome {

Converter create() throws RemoteException, CreateException;
}

Coding the Enterprise Bean Class
The enterprise bean in our example is a stateless session bean called ConverterEJB. This bean implements the two business methods, dollarToYen and yenToEuro, that the Converter remote interface defines. The source code for the ConverterEJB class follows.

import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

public class ConverterEJB implements SessionBean {

public double dollarToYen(double dollars) {

return dollars * 121.6000;
}

public double yenToEuro(double yen) {

return yen * 0.0077;
}

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

Compiling the Enterprise Bean's Source Code
Now you are ready to compile the remote interface (Converter.java), home interface (ConverterHome.java) , and the enterprise bean class (ConverterEJB.java).

Creating the J2EE Application
You cannot deploy an enterprise bean directly into an J2EE server. Instead, you add the bean to a J2EE application, which you then deploy. In this section, you will create a new J2EE application called ConverterApp and store it in the file named ConverterApp.ear.
1. At the command line prompt, start the J2EE server:


j2ee -verbose


(To stop the server, type j2ee -stop.)


2. In another terminal window, run the Application Deployment Tool:

deploytool


(To access the tool's context-sensitive help, press f1.)


3. Create a new J2EE application.

a. In the Application Deployment Tool, select the File menu.
b. From the File menu choose New Application.
c. Click Browse.
d. In the file chooser, locate the directory where you want to place the .ear file that contains the J2EE application.
e. In the File name field enter ConverterApp.ear.
f. Click New Application.
g. Click OK.

Packaging the Enterprise Bean
In this section you will run the New Enterprise Bean Wizard of the Application Deployment Tool to perform these tasks:

Create the bean's deployment descriptor.


Package the deployment descriptor and the bean's classes in an EJB .jar file.


Insert the EJB .jar file into the application's ConverterApp.ear file.


To start the New Enterprise Bean Wizard, from the File menu choose New Enterprise Bean. The wizard displays the following dialog boxes.
Introduction Dialog Box:


a. Read this explanatory text for an overview of the wizard's features.
b. Click Next.
EJB JAR Dialog Box:

a. In the combo box labelled "Enterprise Bean will go in," select ConverterApp.
b. In the JAR Display Name field enter ConverterJAR. Representing the EJB .jar file that contains the bean, this name will be displayed in the tree view.
c. Click the Add button next to the Contents text area.
d. In the Add Contents to JAR dialog box, choose the directory containing the .class files. (This directory is $J2EE_HOME/doc/guides/ejb/examples/converter, where $J2EE_HOME is the location of your J2EE SDK installation.) You may either type the directory name in the Root Directory field or locate it by clicking Browse.
e. Select each of the following classes from the text area and click Add: Converter.class, ConverterEJB.class, and ConverterHome.class.
f. Click OK.
g. Click Next.
General Dialog Box:

a. In the Enterprise Bean Class combo box, select ConverterEJB.
b. In the Home Interface combo box, select ConverterHome.
c. In the Remote Interface combo box, select Converter.
d. Select the Session radio button.
e. Select the Stateless radio button.
f. In the Enterprise Bean Display Name field, enter ConverterBean. This name will represent the enterprise bean in the tree view.
g. Click Next.
Environment Entries Dialog Box:

Because you may skip the remaining dialog boxes, click Finish.

Deploying the J2EE Application
Now that the J2EE application contains an enterprise bean, it is ready for deployment.
1. Specify the JNDI name of the enterprise bean.


a. In the Application Deployment Tool, select ConverterApp in the tree view.
b. Select the JNDI Names tab.
c. In the JNDI Names field, enter MyConverter and press Return. The client will use this name to locate the home interface. (See a later section, "Locate the Home Interface".)
2. Deploy the J2EE application.

a. From the Tools menu, choose Deploy Application.
b. In the first dialog box, verify that the Target Server selection is either "localhost" or the name of the host running the J2EE server.
c. Select the checkbox labelled "Return Client Jar."
d. In the text field that appears, enter the full path name for the file ConverterAppClient.jar. This file will reside in the doc/guides/ejb/examples/converter subdirectory of your J2EE SDK installation.
e. Click Next.
f. In the second dialog box, verify that the JNDI name for the ConverterBean is MyConverter.
g. Click Next.
h. In the third dialog box, click Finish.
i. In the Deployment Progress dialog box, click OK when the deployment completes

Building the Client
The ConverterClient program is a stand-alone Java application. To create ConverterClient you perform these steps:
The full source code for the ConverterClient program follows.

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import Converter;
import ConverterHome;

public class ConverterClient {

public static void main(String[] args) {
try {
Context initial = new InitialContext();
Object objref = initial.lookup("MyConverter");

ConverterHome home =
(ConverterHome)PortableRemoteObject.narrow(objref,
ConverterHome.class);

Converter currencyConverter = home.create();

double amount = currencyConverter.dollarToYen(100.00);
System.out.println(String.valueOf(amount));
amount = currencyConverter.yenToEuro(100.00);
System.out.println(String.valueOf(amount));

currencyConverter.remove();

} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
}

javac -classpath %CPATH% ConverterClient.java

Running the Client

java -classpath "$CPATH" ConverterClient Converter.jar




Any information mail to selestin@aol.in
immediate response or call 9942291292

All the best

Study JAVA J2EE

Read EJB Session Bean Guide post and do EJB with a hour BEST OF LUCK CONTACT 9942291292

click to get job