J2EE programming started

Source: Internet
Author: User
Start of J2EE programming-general Linux technology-Linux programming and kernel information. The following is a detailed description. This document describes how to use EJB to develop, deploy, and run a client/server application. The client in the example runs as a Java independent application and is implemented by the ConverterClient. java class. It implements a simple real-time session, mainly through client applications calling the server-side EJB class ConverterEJB. java. If you have installed J2EE, you can find these examples in the doc/guides/ejb/examples/converter directory.

?? To implement the simple example described in this article, you need to complete the following tasks:

?? · Compile and compile the EJB Program
?? · Create a J2EE Application
?? · Package EJB (. jar)
?? · Deploy J2EE applications
?? · Compile and compile client programs
?? · Run the client

?? The following sections briefly describe the tasks and knowledge points involved.

?? Prerequisites

?? The premise for implementing the example provided in this article is that you have installed the J2SE SDK downloaded from Javasoft.com or the website of the relevant operating system vendor, and installed the corresponding operating system version of J2EE SDK (www.javasoft.com download) and configured correctly. For more information about J2SE and J2EE installation and configuration, see the product documentation.

?? EJB Encoding

?? Each EJB program must write the following code:

?? · Remote interface)
?? · Local interface)
?? · Implementation component (Enterprise bean class)

?? Remote interface code

?? The remote interface defines the commercial methods that can be called by the client. These commercial methods are implemented by the enterprise component of the server. The Converter. java code involved in this article is as follows:

?? Import javax. ejb. EJBObject ;? // Introduce required packages
?? Import java. rmi. RemoteException;

?? Public interface Converter extends EJBObject {? // The EJBObject class must be inherited.
?? /* Define the methods that can be called by the client */
??? Public double dollarToYen (double dollars) throws RemoteException;
??? Public double yenToEuro (double yen) throws RemoteException;
??}

?? Local interface Encoding

?? The Local interface defines the method for creating, searching, or moving ejbs on the client. The ConverterHome interface of the Local interface involved in this article only defines one create method and returns the remote interface type. The encoding is as follows:

?? 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;
??}

?? Enterprise component class (EJB) Encoding

?? The enterprise component (EJB) in this article is a stateless session component named ConverterEJB. java, the component implements two business methods: dollarToYen and yenToEuro, which are consistent with the client access method defined by the remote interface Converter. The class encoding is as 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 ){}
??}

?? Compile EJB

?? Now we need to compile the above three classes. The compilation on the UNIX platform and the NT/9X platform is slightly different. The compilation is as follows:

?? UNIX:

?? 1. In the following script compileEJB. sh, set this to the actual J2EE installation directory.

?? #! /Bin/sh

?? J2EE_HOME =
?? CPATH =.: $ J2EE_HOME/lib/j2ee. jar

?? Javac-classpath "$ CPATH" ConverterEJB. java ConverterHome. java Converter. java

?? 2. Run the compileEJB. sh script.

?? Windows:

?? 1. In the following compileEJB. bat batch file, set this to the actual J2EE installation directory.

?? Set J2EE_HOME =
?? Set CPATH =.; % J2EE_HOME % \ lib \ j2ee. jar

?? Javac-classpath % CPATH % ConverterEJB. java ConverterHome. java Converter. java

?? 2. Run the batch file compileEJB. bat.

?? Create a J2EE Application

?? You cannot directly deploy enterprise components (ejbs) to a J2EE server. You must add the components to a J2EE application before deploying them. In this section, you will create a new J2EE application called ConverterApp and save it as ConverterApp. ear.

?? 1. Start the J2EE server at the command line prompt:

?? J2ee? Verbose
?? (Stop the server command j2ee-stop .)

?? 2. Run the deployment tool in another terminal window:

?? Deploytool
?? (Obtain deployment tool help by pressing F1)

?? 3. Create a New J2EE Application

?? A. Select the "file" menu in the Deployment Tool
?? B. Select "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. Name the file ConverterApp. ear.
?? F. Click "new application"
?? G. Click "OK"

?? Enterprise component (EJB) Packaging

?? In this section, you will run the create EJB wizard of the Deployment Tool to complete the following tasks:

?? · Create a component deployment descriptor
?? · Package descriptors and component classes as. jar files
?? · Package the EJB. jar file to the ConverterApp. ear file of the J2EE Application

?? Select create EJB from the "file" menu to start creating a new EJB. The wizard displays the following dialog box:

?? Introduction dialog box:

?? A. Read the wizard features
?? B. Click "Next"

?? Ejb jar dialog box:

?? A. Select ConverterApp in THE combox marked with "Enterprise Bean will go in.
?? B. Enter ConverterJAR in the JAR Display Name field and declare that the EJB. jar file contains the component. The Name is displayed in the tree structure.
?? C. Click Add content text field
?? D. At the top of the content editing dialog box, enter the directory containing the. class file.
?? E. Select related. class under this directory to add: Converter. class, ConverterEJB. class, and ConverterHome. class.
?? F. Click OK.
?? G. click Next.

?? Comprehensive dialog box:

?? A. Select "session" in the component type"
?? B. Select "stateless"
?? B. Select ConverterEJB In the EJB 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 field.
?? F. click Next.

?? Environment portal dialog box:

?? Because you can skip the subsequent dialog box, you can click Finish to finish creating an EJB application task.

?? Deploy J2EE applications

?? Now the J2EE application contains the enterprise component (EJB), which can be deployed.

?? 1. Specify the JNDI name of the enterprise component

??? A. in the Application Deployment Tool, select ConverterApp in the application tree structure
??? B. Select the JNDI name label.
??? C. In the JNDI Name field, enter MyConverter and confirm. The client uses this name to locate the local interface.

?? 2. Deploy J2EE applications

??? A. Select "deployment" from the tool menu"
??? B. In the first dialog box, select ConverterApp as the deployment object and Localhost as the target server.
??? C. Select the selection box marked with "Return Client Jar ."
??? D. Enter the exact ConverterAppClient. jar directory in the text field, for example, the J2EE example directory doc/guides/ejb/examples/converter
??? E. click Next.
??? F. In the second dialog box, verify that the JNDI name of ConverterBean is MyConverter.
??? G. click Next.
??? H. Click Finish in the third dialog box.
??? I. In the deployment process dialog box, click OK to complete the deployment.

?? Development Client

?? The client ConverterClient is a Java application that runs independently. To create a ConverterClient, perform the following steps:

?? 1. Client Encoding
?? 2. Client Compilation

?? Client code

?? ConverterClient. java source code shows the most basic task implementation of the EJB client, namely:

?? · Locating local interfaces
?? · Create an enterprise component (EJB)
?? · Call commercial methods

?? Locate local Interface

?? The ConverterHome interface defines the EJB declaration cycle method such as create. Before ConverterClient can call the create method, the ConverterHome object must be instantiated. Three steps are involved:

?? 1. Create a JNDI naming context

?? Context initial = new InitialContext ();

?? 2. Search for objects bound to the JNDI name MyConverter

?? Java. lang. Object objref = initial. lookup ("MyConverter ");

?? 3. restrict references pointing 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 calling the create method of the Local interface ConvertHome object. The create method returns the Converter type object of the component. Then, the commercial method defined by the remote method implemented in ConverterEJB can be called by the client. When the client calls the create method, the EJB container will instantiate ConverterEJB and then call the ConverterEJB. ejbCreate method.

?? Converter currencyConverter = home. create ();

?? Call commercial methods

?? After completing the preceding tasks, it is easy to call commercial methods. You call the Converter object method. The EJB container calls the ConverterEJB instance method running on the J2EE server. The code for the client to call the commercial method dollarToYen is as follows:

?? Double amount = currencyConverter. dollarToYen (100.00 );

?? ConverterClient source code

?? The complete source code of ConverterClient. java is as 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. valuesOf (amount ));
????? Amount = currencyConverter. yenToEuro (100.00 );
????? System. out. println (String. valuesOf (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, modify the actual installation directory of J2EE.

?? #! /Bin/sh

?? J2EE_HOME =
?? CPATH =.: $ J2EE_HOME/lib/j2ee. jar

?? Javac-classpath "$ CPATH" ConverterClient. java

?? 2. Run the script file compileClient. sh

?? Windows:

?? 1. Modify the actual J2EE installation directory in the following batch processing file compileClient. bat.

?? Set J2EE_HOME =
?? Set CPATH =.; % J2EE_HOME % \ lib \ j2ee. jar

?? Javac-classpath % CPATH % ConverterClient. java

?? 2. Run the batch processing file compileClient. bat

?? Run the client

?? To run the client, you need the ConverterAppClient. jar file, which contains the stub class required to allow the client to communicate with the EJB instance in the EJB container. The ConverterAppClient. jar file is created during J2EE application deployment.

?? UNIX:

?? 1. In the following script testClient. sh, modify the actual installation directory of J2EE.

?? #! /Bin/sh

?? J2EE_HOME =
?? CPATH = $ J2EE_HOME/lib/j2ee. jar: ConverterAppClient. jar :.

?? Java-classpath "$ CPATH" ConverterClient

?? 2. Run the script file testClient. sh.

?? Windows:

?? 1. Modify the actual J2EE installation directory in the following batch processing file testClient. bat.

?? Set J2EE_HOME =
?? Set CPATH =.; % J2EE_HOME % \ lib \ j2ee. jar; ConverterAppClient. jar

?? Java-classpath "% CPATH %" ConverterClient

?? 2. Run the batch processing file testClient. bat

?? Troubleshooting

?? The following error is reported when you run ConverterClient:

?? 1. java. lang. ClassCastException

?? The ConverterAppClient. jar file may not be found.

?? 2. java. lang. NoClassDefFoundError: ConverterClient

?? The ConverterClient. class file cannot be located.

?? 3. java. lang. NoClassDefFoundError: javax/naming/Context

?? Unable to find the required j2ee. jar file and confirm the CLASSPATH settings.

?? 4. javax. naming. NameNotFoundException: Lookup of name MyConverter failed.

?? The J2EE server cannot locate the component bound to the JNDI name MyConverter.

?? 5. javax. naming. NamingException: Error accessing repository: Cannot connect to ORB ....

?? The J2EE server is not running.

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.