Java EE programming start (ii)

Source: Internet
Author: User
Tags contains modify
j2ee| programming to create Java EE application





You cannot deploy an enterprise component (EJB) directly to the Java server, you must add the component to a Java application and then deploy it. In this section, you will create a new Java EE application called Converterapp and save it as a converterapp.ear.





1. Start the Java server at the command prompt:





EE verbose


(Stop Server command j2ee-stop.)





2. To run the deployment tool in another terminal window:





Deploytool


(press F1 to get deployment Tool Help)





3. Create a new Java EE application





A. Select the File menu in the Deployment tool


B. Choose New Application from the File menu


c. Click to browse


d. Locate the directory where the. ear file is stored in the File selection box


E. File is named Converterapp.ear.


F. Click "New Application"


g. Click "OK"





Enterprise Component (EJB) packaging





This section you will run the Create EJB Wizard for the deployment tool to complete the following tasks:





Create a component deployment descriptor


package descriptors and component classes as. jar Files


ejb.jar file to Java EE application converterapp.ear file





choose to create an EJB from the File menu to start creating a new EJB, the wizard displays the following dialog box:





Introduction dialog box:





A. Reading Wizard feature description document


B. Click "Next"





EJB JAR dialog box:





A. Select Converterapp in the combo box labeled "Enterprise Bean would go in."


B. Enter Converterjar in the jar Display Name field and declare the EJB. jar file contains the component that will be rendered in the tree application structure.


C. Click the Add content Text field


D. At the top of the content Editing dialog box, enter the directory containing the. class file


E. Select the directory related to. Class join: Converter.class, Converterejb.class, and Converterhome.class.


F. Click OK


G. Click Next





Comprehensive dialog box:





A. Select the session
in the component type

B. Select "Stateless"


B. Select CONVERTEREJB in the EJB class combo box.


c. Select Converterhome in the local interface combo box.


d. Select Converter in the Remote interface combo box.


E. Enter Converterbean in the Enterprise component naming domain.


F. Click Next





Environment Portal dialog box:





because you can skip the following dialog box, you can click Finish to create an EJB application task.





Deployment Java Application





Now the Java EE application already contains an enterprise component (EJB) that can be deployed.





1. Specify the Jndi name of the Enterprise component


A. In the application of the deployment of agricultural tools, in the application of tree-shaped structure to select Converterapp


B. Select Jndi name Tag


c. In the Jndi name field, enter Myconverter and confirm. The client will use that name to locate the local interface.





2. Deploying Java EE application


A. Choose Deploy from the Tools menu


B. In the first dialog box, select Converterapp as the Deployment object, localhost as the target server


c. Select the box labeled "Return Client Jar."


d. Converterappclient.jar the exact directory in the text field, such as the example directory of Java EE doc/guides/ejb/examples/converter


E. Click Next


F. In the second dialog box, verify that the Converterbean Jndi is named Myconverter.


G. Click Next


H. In the Third dialog box, click End


I. In the Deployment Process dialog box, click OK to complete the deployment





Development Client





client program Converterclient is a stand-alone Java application, creating converterclient consists of the following steps:





1. Client-side encoding


2. Client-side compilation





client-side encoding





Converterclient.java Source shows the EJB client the most basic task implementation, namely:





Location Local interface


Create an Enterprise component (EJB)


Calling Business methods





Location Local Interface




The
Converterhome interface defines the EJB declaration cycle method such as create, in which the Converterhome object must be instantiated before converterclient can call the Create method, including three steps:





1. Creating a Jndi Naming context


Context initial = new InitialContext ();





2. Find the object that is bound to the Jndi named Myconverter


java.lang.Object ObjRef = Initial.lookup ("Myconverter");





3. Limit references to Converterhome objects


Converterhome home =


(converterhome) Portableremoteobject.narrow (ObjRef,


Converterhome.class);





Create an Enterprise component (EJB) instance




The
client creates a CONVERTEREJB class object by invoking the Create method of the local interface Converthome object, and the Create method returns an object of the component converter type. The business method that the remote method defines in the CONVERTEREJB implementation can then be invoked by the client. When the client invokes the Create method, the EJB container instantiates the CONVERTEREJB, and then calls the Converterejb.ejbcreate method.





Converter currencyconverter = Home.create ();





Invoke Business Method





After the above task is done, it is simpler to invoke the business method. You invoke the method of the converter object, and the EJB container will call the method of the CONVERTEREJB instance running in the Java server. The client invokes the business method Dollartoyen code as follows:





Double amount = Currencyconverter.dollartoyen (100.00);





converterclient Source





Below is Converterclient.java's complete source code:





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 ();


}


}


}











Compile client code





UNIX:





1. In the following script compileclient.sh, the actual installation of the Java EE directory to make the corresponding changes.





#!/bin/sh


j2ee_home=


cpath=.: $J 2ee_home/lib/j2ee.jar








Javac-classpath "$CPATH" Converterclient.java





2. Run script file compileclient.sh





Windows:





1. In the following batch file Compileclient.bat, modify the installation directory according to the actual Java EE.





Set J2ee_home=


set cpath=.; %j2ee_home%\lib\j2ee.jar





Javac-classpath%cpath% Converterclient.java





2. Run batch file Compileclient.bat





Run Client





run the client you need a Converterappclient.jar file that contains the stub class required to allow the client to communicate with an EJB instance in the EJB container, Converterappclient.jar files are created during deployment of Java application.





UNIX:





1. In the following script testclient.sh, the actual installation of the Java EE directory to make the corresponding changes.





#!/bin/sh


j2ee_home=


cpath= $J 2ee_home/lib/j2ee.jar:converterappclient.jar:.


Java-classpath "$CPATH" Converterclient





2. Run script file testclient.sh





Windows:





1. In the following batch file Testclient.bat, modify the installation directory according to the actual Java EE.





Set J2ee_home=


Set cpath=.; %j2ee_home%\lib\j2ee.jar; Converterappclient.jar


Java-classpath "%cpath%" Converterclient





2. Run batch file Testclient.bat





Frequently asked questions handling





found the following error while running Converterclient:





1. Java.lang.ClassCastException


may not be able to find the Converterappclient.jar file.





2. Java.lang.NoClassDefFoundError:ConverterClient


cannot locate Converterclient.class file.





3. Java.lang.noclassdeffounderror:javax/naming/context


could not find the required J2ee.jar file to confirm classpath settings.





4. Javax.naming.NameNotFoundException:Lookup of name Myconverter failed.

The
Java server cannot locate the component that the Jndi name Myconverter is bound to.





5. Javax.naming.NamingException:Error accessing Repository:cannot connect to ORB at ....


Java server is not running.








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.