Develop the first distributed J2EE Application

Source: Internet
Author: User



I Love Tiananmen, Beijing


Sun rises on Tiananmen


Great leader Chairman Mao


Guide us forward


<Font size = "2"> here, we first reference the Department content in installing J2EE in Charly windows and deploying J2EE applications <br/> 1. install <br/> A j2ee(j2sdkee-rj3-beta2-win.exe): Large. You may have installed the old version of J2EE SDK. If yes, uninstall or delete the old version of J2EE SDK before installing the new J2EE SDK. Run j2sdkee-1_3-beta2-win.exe and install J2EE according to the installation steps. Assume that your J2EE is installed in: C:/j2sdkee1.3.
Directory. <Br/> 2. set environment variables <br/> before running the J2EE SDK, you must set the following environment variables: <br/> j2ee_home-the directory where your J2EE SDK is installed. In this example, C:/j2sdkee1.3. <Br/> java_home-the directory where your Java 2 SDK is installed. <Br/> path-set the bin directory under the J2EE SDK installation directory. In this example, C:/j2sdkee1.3/bin. <Br/> Add % j2ee_home %/lib/J2EE. jar to classpath. In this example, you can also enter C:/j2sdkee1.3/lib/J2EE. Jar <br
/> <Br/> 3. run the J2EE <br/> doscommand line and type the following command: <br/> % j2ee_home %/bin/J2EE-verbose <br/> The following information indicates that the operation is successful: (different versions may be displayed differently) <br/> J2EE server listen Port: 1050 <br/> Naming Service started: 1050 <br/> binding datasource, name = JDBC/estoredb, url = JDBC: cloudscape: RMI: cloudscapedb; Create = true <br
/> Binding datasource, name = JDBC/DB2, url = JDBC: cloudscape: RMI: cloudscapedb; Create = true <br/> binding datasource, name = JDBC/cloudscape, url = JDBC: cloudscape: RMI: cloudscapedb; Create = true <br/> binding datasource, name = JDBC/inventorydb, url = JDBC: cloudscape: RMI: cloudscapedb; create = true <br
/> Binding datasource, name = JDBC/db1, url = JDBC: cloudscape: RMI: cloudscapedb; Create = true <br/> binding datasource, name = JDBC/xacloudscape, url = JDBC/xacloudscape _ XA <br/> binding datasource, name = JDBC/xacloudscape _ XA, datasource = com. cloudscape. core. remotexadatasource @ 330913 <br
/> Starting JMS service... initialization complete-waiting for client requests <br/> binding: & lt; JMS destination: JMS/queue, javax. JMS. queue & gt; <br/> binding: & lt; JMS destination: JMS/topic, javax. JMS. TOPIC & gt; <br/> binding: & lt; jms cnx Factory
: JMS/topicconnectionfactory, topic, no Properties>
Binding: <jms cnx Factory: topicconnectionfactory, topic, no Properties>
Binding: <jms cnx Factory: JMS/queueconnectionfactory, queue, no Properties>
Binding: <jms cnx Factory: queueconnectionfactory, queue, no Properties>
Starting web service at Port: 8000
Starting Secure Web Service at Port: 7000
Apache Tomcat/4.0-b4-dev
Starting web service at Port: 9191
Apache Tomcat/4.0-b4-dev
J2EE server startup complete.

After the startup is successful, you can access http: // localhost: 8000 in the IE browser to see the recognized main page message.

4. Write and run the helloworld Program
J2EE applications generally use RMI (remote method call) to complete the interaction between the client and the server. Of course, the role of EJB is indispensable. This example is a J2EE application: the client sends a greeting "Hello, remote object" to the server ". After receiving the greeting, the server prints the greeting and returns a string as the response. The client prints the response after receiving the response.

Remoteinterface. Java

/**
* Step 1:
* Define a new interface to inherit javax. EJB. ejbobject. Every method in the newly defined interface must be thrown
* Java. RMI. RemoteException.
*/
Public interface remoteinterface extends javax. EJB. ejbobject
{
Public String message (string Str) throws java. RMI. RemoteException;
}

Remoteobject. Java

/**
* Step 2:
* Define a class to implement the javax. EJB. sessionbean interface. The method defined in the interface written in step 1 is implemented in this class.
*/
Public class remoteobject implements javax. EJB. sessionbean
{
Public String message (string Str) throws java. RMI. RemoteException
{
System. Out. println ("Remote Object received ed from client:/" "+ STR +"/""); // print the string received (from the client.
Return "Hello, I'm remote object, I got ed your message:/'" + STR + "/'"; // return a response string.
}

Public remoteobject (){}
Public void ejbcreate (){}
Public void ejbremove (){}
Public void ejbactivate (){}
Public void ejbpassivate (){}
Public void setsessioncontext (javax. EJB. sessioncontext SC ){}
}

Remotehome. Java

/**
* Step 3:
* Define a class to inherit javax. EJB. ejbhome.
*/
Public interface remotehome extends javax. EJB. ejbhome
{
Remoteinterface create () throws java. RMI. RemoteException, javax. EJB. createexception;
}

Client. Java

/**
* Step 4:
* Define the client class.
*/
Public class client
{
Public static void main (string [] ARGs)
{
Try
{
Javax. Naming. Context initcontext = new javax. Naming. initialcontext ();
Object OBJ = initcontext. Lookup ("helloworld"); // you can call this operation to obtain the corresponding object by name.
Remotehome home = (remotehome) javax. RMI. portableremoteobject. Narrow (OBJ, remotehome. Class );
Remoteinterface remote = home. Create ();
String receivefromremote = remote. Message ("Hello, remote object! "); // Remote method call
System. Out. println ("client received ed from remote object:/" "+ receivefromremote + "/"");
}
Catch (exception E)
{
E. printstacktrace ();
}
}
}

Assume that the above four java files are stored in C:/helloworld/, and compile them, for example, C:/helloworld> javac *. java.

5. Deploy the application
Start application dopolyment tool: open a DOS window and type the following command, % j2ee_home %/bin/deploytool. The tool may be slow to start, so be patient. The main interface will appear after the startup is successful (do not close the DOS window at this time ). Select the File menu in the interface, and then select the new application item. Enter C:/helloworld. Ear in application file name. Enter your favorite display name such as helloworld in application disply name. Point
Click OK and add helloworld under the main interface tree structure files --> applications. This means that a new application is created. Next, we need to deploy the application. Select helloworld in the tree structure of the main interface, and then select new --> enterprise Bean in the File menu of the main interface, in the pop-up window "new enterprise Bean-Introduction", select next to skip the first step. In the next step, select helloworld in the create new EJB file in application item and
Enter your favorite name in display name, such as Hello World EJB. Click Edit. In the displayed window, enter C:/helloworld/in start directory /, expand the tree structure C:/helloworld/in available files, and select remoteinterface. class, remoteobject. class, remotehome. class, client. class four items, click the Add button to add, and then press OK to confirm. Add the four classes in the contents box. Click Next to go to the next step. If stateless is selected for the session item, the session state is not saved. Enterprise
Select remoteobject for Bean class. Enter your favorite name in enterprise Bean name, such as Hello World bean. Select remotehome in the remote home interface and remoteinterface in the remote interface. Select next to go to the next step. You can directly click Finish in the following steps. In the tree structure of the main interface, the hello World EJB --> Hello World bean subitem will appear in files --> Application --> Hello World bean. Select Hello under the tree structure of the main interface
And then select deploy from the Tools menu on the main interface. A new window named "deploy Hello world-Introduction" is displayed ". Select "Hello World" for "object to deploy", "localhost" for "target server", and "retuen client jar", and enter "C:/helloworld/helloworldclient. Jar" in "client JAR file name. Select next to go to the next step. Double-click the JNDI Name box in the application box and fill in helloworld.
OBJ = initcontext. Lookup ("helloworld") "helloworld" is consistent. Click Next to go to the next step. Click Finish. The deployment SS window appears. If an error occurs, the window displays an exception. If everything is normal, click OK to complete the deployment.

6. Run the application
Open a new DOS window. Go to the C:/helloworld/classes directory and run: C:/helloworld/classes> JAVA-classpath % j2ee_home %/lib/J2EE. jar; ..; helloworldclient. jar; client. The following message is displayed when running successfully: client received ed from remote object: "Hello, I'm remote object, I got ed your message: 'Hello, remote object! '"
. The following message is displayed in the Server DOS window (J2EE-verbose): Remote Object received ed from client: "Hello, remote object! ".

Okay. Now everyone should have started a real J2EE program?
But now there is a question: What are the advantages of EJB and J2EE? What is distributed? What is the distributed system on a machine? Isn't it a bit ridiculous? Now I will teach you how to turn the above program into a distributed one.
If you copy the client. Class directly to another machine, it cannot be executed. Because you still need a home and remote interface, where are these two files? In the helloclient. jar package! :) So you still need this package. Copy the package and execute it. Because your JNDI name is just a name, how does the program know where the server you want to connect to is? Therefore, you need to make the following changes to the original client code.
Change

Properties Env = new properties ();
Env. Put (javax. Naming. Context. initial_context_factory, "com. Sun. JNDI. cosnaming. cnctxfactory ");
Env. Put (javax. Naming. Context. provider_url, "IIOP: // wcy: 1050 ");
// Javax. Naming. Context initcontext = new javax. Naming. initialcontext (ENV );
Context initcontext = new initialcontext (ENV );

Object OBJ = initcontext. Lookup ("helloworld"); // you can call this operation to obtain the corresponding object by name.
Remotehome home = (remotehome) javax. RMI. portableremoteobject. Narrow (OBJ, remotehome. Class );
Remoteinterface remote = home. Create ();
String receivefromremote = remote. Message ("Hello, remote object! "); // Remote method call
System. Out. println ("client received ed from remote object:/" "+ receivefromremote + "/"");

After javac *. Java is compiled successfully. Copy the new class to another machine.
Enter Java-classpath % j2ee_home %/lib/J2EE. jar;.; helloworldclient. jar; Client

In this way, the operation is successful. Excited? Haha ~~ Please leave a message if you have any other questions.

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.