J2EE's client

xiaoxiao2021-03-06  23

J2EE's client

content:

J2EE Client Profile Client How to Access EJBStand Alone ClientJ2ee Application Client Appendix 1 J2EE SDK1.3 Introduction Appendix 2 - Release HelloWorld Stales Session Bean Appendix 3 - Create J2EE Application Client Components About the author

Other related Java Zone:

Teaching tools and product code and components all articles practical skills

Wang Xiaoqiang forff@sina.com) December 2001

This article tried to introduce the J2EE system from the perspective of the J2EE client. How to introduce the client of J2EE accesses EJB. J2EE's client classification. Last focus analysis J2EE two similar client Stand Alone Client and J2EE Application Client. And will give specific examples of these two different clients of the same STATELESS SESSION bean to specifically explain their excessive differences.

All instances of this article are published based on Sun's J2EE SDK. The appendix will also learn this very convenient development of J2EE for introduction. J2EE Client Introduction J2EE's client, simple to say that all components and programs that are in customer call logic for EJBs. Because of the complexity of the J2EE structure, the J2EE client is also more, generally divided into the following five. Stand Alone Client, J2EE Application Client, JSP, Servlets, other Enterprise JavaBeans (in customer logic EJB). Among them, everyone may be more familiar to JSP and servlets, because now J2EE-based applications are mostly Broswer / Server mode, they are also the most commonly used J2EE client. The EJB itself plays a client role, and we will often meet, such as the business method in the Entity Bean in the session bean, then the session bean is the client of this Entity Bean. When you talk about J2EE, you can't mention EJB. EJB is the core of the J2EE structure, we implements business logic in it, and the service platform provider that implements J2EE structures provides us with J2EE Server, EJB Container, Web Container, thus providing us with security control, transaction, customer connection And database access these services. This makes it possible to achieve the realization of commercial logic and maximize the reuse of the business logic and maximize code. There are two EJBs in the EJB1.1 specification, one is the other of the session bean is Entity Bean. These two EJBs are also our most common. Whether it is session bean or an Entity bean they are implemented by three parts. The first is the Remote Home Interface, which is defined in this remote interface that can be created by the client, lookup (for Entity Bean) EJB method. Then the Remote Interface, which is defined in this remote interface is a business approach to the client access. Finally, bean class, this class is irreparable to the client, in which we have to implement the corresponding business methods, as well as some methods for the EJB Container call. This category will have a big difference to the Session Bean and the Entity Bean, and the BMP mode in the Entity Bean, and CMP mode. But this is not this article to focus on, you only have to remember the client of J2EE, you can see that Remote Interface and Remote Home Interface are both interfaces. Let's take a look at how the client is specific to EJB access. How does the client access to EJB Whether it is the J2EE client, it is necessary to call an EJB's corresponding business method to pass the following three steps: 1. Create a JNDI name environment through the JNDI positioning EJB Remote Home Interface, by publishing You find the JNDI name defined by the EJB to find the EJB Remote Home Interface. 2. Create an instance of EJB, get the Crete () method in the Remote Interface call last Remote Home Interface, EJB Container creates an instance of the corresponding EJB. What you get is a Remote Interface that defines the business method of EJB to implement.

(EJB Container Creating the basic process of the EJB instance is as follows: Client calls create () method-'EJB Container instantiates the corresponding EJB Bean Class'EJB Container calls the EJBCREATE method in the bean class' The last return to us is EJB Remote Interface 3, calling the business method client in Remote Interface Calls the business method in the previously created Remote Interface, EJB Container calls the corresponding method in the corresponding bean class instance. Let us explain this process through the specific code. First let's create a Stateless session bean. This session bean implemented very simple, only one Sayhello method in the business method, print a "Hello World:". As I have three parts, the EJB I have introduced, their source code is shown below: Remote Home Interface interface including the Create () method: package com.javausr.example;

Import java.rmi. *;

Import javax.ejb. *;

Public interface helloworldhome extends ejbhome {

Public HelloWorld Create () THROWS RemoteException, CreateException

} Remote Interface interface for defining business methods:

Package com.javausr.example;

Import java.rmi. *;

Import javax.ejb. *;

Public interface helloworld extends ejbobject {

Public void sayhello () throws remoteException;

} Bean class of business method:

Package com.javausr.example;

Import java.rmi. *;

Import javax.ejb. *;

Public class helloworldbean imports sessionbean {

Private sessionContext sessionContext;

Public void ejbcreate () {}

Public void ejbremove () THROWS RemoteException {}

Public void ejbactivate () throws remoteexception {}

Public void ejbpassivate () throws remoteexception {}

Public void setsessionContext (sessioncontext sessioncontext) THROWS

RemoteException {

This.SessionContext = sessionContext;

}

/ ** Unique business method * /

Public void syhello () {

System.out.println ("Hello :)");

}

} After the specific EJB, we can see how the J2EE client is specific to finish three steps. First, we get the HelloWorldHome interface through JNDI, which is divided into the following steps:

Create a name environment for JNDI: context ctx = new initialContext (); I am using Sun's J2EE SDK1.3 release. In this release environment, the name environment that creates JNDI does not require any parameters, because these parameters are automatically set when the system is started. However, if you use WebLogic or other application servers, you need to specify context.initial_context_factory and context.provider_URL parameters to create this initial name environment (with JNDI details, you can refer to Sun's JNDI section). Get the JNDI name of the EJB when released, get the object we need: Object ref = ctx.lookup ("HelloWorld"); here I gave this EJB JNDI name is HelloWorld. Now we want to step on the resulting object model to HelloWorldHome objects: HelloWorldHome helloWorldHome = (HelloWorldHome) PortableRemoteObject.narrow (ref, HelloWorldHome.class); because get is a remote object, so here we use the javax.rmi.PortableRemoteObject Methods The objects we got will be styled. After we get the EJB Remote Home Interface, we create an EJB instance by () method, and get Remote Interface. The code is as follows: helloworld HelloWorld = HelloWorldHome.create (); Now we can call the business method we want to call through the Remote Interface you get. Here our EJB only implements a SayHello () method. Complete the call to it with the following code: helloworld.sayhello (); all client access EJB is to pass through these steps. Servlets typically complete the steps of creating an EJB instance in its init () method. JSP accesses EJB, it is best to be implemented through Java Beans, so the steps of creating EJBs are generally completed in the initialization method of the corresponding Java Bean. Of course, you can also complete these steps anywhere according to your needs. This article tries to introduce you to the J2EE structure from the perspective of the client, not only to the client. So there are some other information here, I hope everyone can understand. In the EJB2.0 specification, some new situations can be accessed local access (LOCAL Access) for EJB. The EJB that implements local access is like implementing the EJB of remote access to the client access. One is Local Interface that defines the business logic to be implemented like Remote Interface. One is Local Home Interface, which provides creation EJB and finds (for entity beans), just like Remote Home Interface. These two Local Interface are used for client-side access, and the implementation of EJB is still in the bean class. For the client, if it is going to access an EJB with only local interface, it must be in the same Java virtual machine with this EJB. And for an EJB that implements a remote interface, there is of course not such a limit. Local access will increase system performance, and in some fields and you must make EJBs to achieve local access (such as entry-managed Relationship Entity Bean, please refer to EJB2.0 specification for details. Generally only implemented local access interfaces in Entity Beans ). When you design an EJB, you should consider whether you are remote or local access.

If you have all the components such as EJB, Web, J2EE Client, etc., just published on the same machine, then you can choose to implement local interfaces for EJBs (of course, some must implement local interfaces). If you have to consider distributed, the remote access interface is your unique choice. If your EJB uses local access, there are some new changes to the client. The client access implements the process of the local interface EJB and the access to the above-described access to the remote interface EJB is basically the same. However, after the EJB object is obtained by EJB, it is necessary to use the method of javax.rmi.PortableRemoteObject if it is used as the Local Home interface of the corresponding EJB. Suppose we implement the EJB of the EJB is local access interface, then the corresponding client access code is as follows: context ctx = new initialContext ();

Object ref = ctx.lookup ("HelloWorld");

HelloWorldHome HelloWorldHome = (HelloWorldHome) REF;

There is also a new EJB: Message-Driven Bean in EJB2.0. This new EJB type is very different from other two. First, it is not like other two EJBs that have its own Remote or Local Interface for client calls, which only has a bean class. Moreover, the client is also unable to position and call the method in the Message-Driven Bean. It is invisible to the client. Let's take a detailed introduction to the two clients in J2EE, Stand Alone Client and J2EE Application Client. Stand Alone Client This client is like its name, which is independent of J2EE, not J2EE component. It is generally used to test the EJB we developed. Below is a specific example, it accessed EJB is the STATELESS session bean on the upper side.

Import javax.naming.context;

Import javax.naming.initialcontext;

Import javax.rmi.portableremoteObject;

Import com.javausr.example.helloworld;

Import com.javausr.example.helloworldhome;

Public class myStandaloneClient {

Public static void main (String [] args) {

Try {

// Create a JNDI initialization environment

Context initial = new initialcontext ();

/ / Find the REMOTE HOME INTERFACE of EJB

Object objref = initial.lookup ("HelloWorld");

HelloWorldHome Home =

(HelloWorldHome) PortableRemoteObject.narrow (Objref,

HelloWorldHome.Class;

// Initialize EJB to get Remote Interface

HelloWorld HelloWorld = Home.create ();

// Call the business method SayHello ()

HelloWorld.SAYHELLO ();

System.exit (0);

} catch (exception ex) {

System.err.Println ("Caught An Unexpected Exception!");

EX.PrintStackTrace ();

}

}

} Open a DOS window, compile our code using the following instructions (please adjust according to the parameters of your environment): d: /j2sdkee1.3/myapp/hellow ~ 1> javac -classpath.; D: / J2SDKEE1.3 / lib / j2ee.jar MyStandaloneClient.java Before running this example, you want to release the EJB we implemented on our J2EE server. Here I use the J2EE server is J2EE SDK 1.3, please refer to Appendix 1 - J2EE SDK1.3 of this article for introduction. For detailed steps to release HelloWorld EJB on it, see Appendix 2 - Release HelloWorld Stales Session Bean. In the case where the server is already started, the EJB has been released, let's run an example of the upper side, run using the following command: d: /j2sdkee1.3/myapp/hellow ~ 1> java -classpath ".; D: / j2sdkee1 .3 / lib / j2ee.jar; HelloWorldClient.jar "MyStandaloneClient This can you see" Hello :) "printed by our MyStandaloneClient calls on the J2EE Server console (using the J2EE -Verbose Start Server). J2EE Application Client It and Stand Alone Client are very similar to a Java Application, but different is one of the J2EE components. So want to use this client, you have to publish it to the server with the application publishing tool of the corresponding J2EE server. Because it is a J2EE component, it is different from the Stand Alone Client that it can use the service provided by J2EE. For example, it can be securely authenticated by J2EE. When you run the J2EE Application Client, a authentication window will automatically pop up. After you enter the username and password licensed on the J2EE server, you can run this program to access the EJB on J2EE Server. It is impossible to give us the benefits of developing us. Also in the J2EE Application Client, you can access the EJB with your own JNDI name by adding JNDI reference for existing EJBs. For example, in my example is to get an EJB object by the following code: Object objref = initial.lookup ("java: comp / eNV / EJB / HAI); I may remember that in the Stand Alone Client example, I passed "HelloWorld" JNDI name gave the same object. And here is "EJB / HAI". Use different JNDI names to access the same EJB. The advantage of this is that you can change your EJB JNDI name, and J2EE Application Client program code does not need to have any changes, just modify the correspondence in the EJB reference. Here is the source code: import javax.naming.context;

Import javax.naming.initialcontext;

Import javax.rmi.portableremoteObject;

Import com.javausr.example.helloworld;

Import com.javausr.example.helloworldhome;

Public class myj2eeppclient {

Public static void main (String [] args) {

Try {

// Create a JNDI initialization environment

Context initial = new initialcontext (); // Find EJB Remote Home Interface

Object objref = initial.lookup ("Java: Comp / ENV / EJB / HAI");

HelloWorldHome Home =

(HelloWorldHome) PortableRemoteObject.narrow (Objref,

HelloWorldHome.Class;

// Initialize EJB to get Remote Interface

HelloWorld HelloWorld = Home.create ();

// Call the business method SayHello ()

HelloWorld.SAYHELLO ();

System.exit (0);

} catch (exception ex) {

System.err.Println ("Caught An Unexpected Exception!");

EX.PrintStackTrace ();

}

}

}

You will find that there is no other difference in the code from the JNDI name mentioned above. But we can't run it like a Stand Alone Client, and you must create a J2EE Application Client component and add it to our app. For this creation process, please refer to Appendix 3 - Create a J2EE Application Client component. Create a J2EE Application Client component and publish our app, you can run our J2EE Application Client. Here we need to use J2EE SDK1.3 to provide the batch command running the J2EE Application Client to run the J2EE Application Client (in the / bin directory, copy it to the directory containing the compiled class file). Finally, we use the following command to run: d: /j2sdkee1.3/myapp/hellow ~ 1> runclient -client helloworld.ear -name j2eeappclient This time a authentication window, enter the username guest and password guest 123 (the system default User):

Then you can see the result at J2EE Server's console, output hello :). Corresponding to the console we run J2EE Application Client will also print out the following information during the execution:

Initiarating login ...

UserName = NULL

Binding name: `java: comp / env / ejb / hai`

Unbinding name: `java: comp / env / ejb / hai`

From the release of the J2EE Application Client component, and the running process, you have seen it with the two biggest differences in Stand Alone Client, one is we reloading another JNDI reference name "hai" to access EJB, the other is running The system will be certified for us. J2EE Application Client can have many practical purposes, such as using it to implement background management functions in our system. This article is some of my own experience when I study J2EE. For J2EE, the core is of course EJB, especially EntityBean. I hope that this article can help everyone from another angle. If you have any questions, please contact me for FORFF@sina.com. Appendix 1 J2EE SDK1.3 Introduction J2EE SDK 1.3 is the latest implementation of Sun's J2EE specification, which contains J2EE Application Server and a large number of other very useful development tools. It supports the following techniques: HTTP and HTTPS COS naming Enterprise JavaBeansTM (EJB) 2.0 JavaTM Servlets 2.3 JavaServer PagesTM (JSP) 1.2 JavaTM Messaging Service (JMS) 1.0.2 J2EETM Connector 1.0 JDBCTM Standard Extension 2.0 JavaTM Transaction API (JTA) 1.0 JavaMailTM 1.2 JavatM API for XML Parsing (JAX) 1.1 and it also supplies a pure Java database system - Cloudscape. So you can easily develop a set of applications with J2EE all the latest features in a single-machine system, do not need to be a hard drive, you can't see the system response (imagine, you are loading Oracle on a PC, then installing What is the feeling after another commercial server? Of course, if your machine is too good, it is another matter, but my PIII600 128 megabula is mounted. This is a very good learning tool for developers who have just started touching J2EE :). It allows us to focus more on the technology itself, not the development environment. You can get it from Sun. After installation, we have to do some configuration work. There are many boot commands and tools in / BIN. Here we have to use: UserConfig.bat: Generally we configure environmental parameters here. Here is Java_Home (see the installation path of your JDK) and J2EE_HOME (J2EE SDK 1.3). Configure them before starting the server before starting the server, initialize environmental parameters. Of course, you can also use your own batch command, but isn't it more convenient to use the system? My file is as follows:

Set java_home = c: /jbuilder5/jdk1.3

Set J2EE_HOME = D: /J2SDKEE1.3

J2EE.BAT: Start the command of J2EE Server. Generally, you can start the service directly under the command prompt. Here we use J2EE -VERBOSE to launch the server so that our information can be displayed on the console. Deploytool.bat: Tools for constructing and publish J2EE applications. If you type directly into the deploytool command system, you will start a visual publishing tool. Runclient.bat: A tool for us to run J2EE Application Client. After we configure the environmental parameters, you can complete the appropriate work as long as you start these commands directly. For details on J2EE SDK 1.3, please refer to its documentation (under / doc). Appendix 2 - Release HelloWorld Stales Session Bean Let's see how this session bean is released in J2EE SDK1.3. Step 1: Create a J2EE application in J2EE SDK 1.3, you can't directly release EJB. You need to add EJB to an existing J2EE application and then release it. Here we first create a new application called J2EECLIENTTEST. This app is ultimately saved as HelloWorld.ear. The following is a specific step: 1, open a DOS window, start the J2EE server. J2EE -VERBOSE (only in this mode is started, can we see the output in the program) 2, open a DOS window, start the application publishing tool: Deploytool This command will start a release tool as shown below: 3, Let's take a new J2EE app. A. Select [File] -> [New] -> [Application] in the menu shown in the figure above. Or directly select the New Application shortcut on the window. B, a dialog window will pop up. In the Application File Name input box, enter the file name of the application you save: *. EAR, I use HelloWorld.ear here. In the Application Display Name Enterprise Enter the display name of this app, I use J2EECLIEntTest here. We have created a new application after selecting OK. 4, below we joined our EJB components in this new application. A, compile the 3 classes of our EJB, we use the following commands in the DOS window: D: /j2sdkee1.3/myapp/hellow ~ 1> javac -classpath;.; D: /j2sdkee1.3/ LIB / J2EE.JAR -D. HelloWorldHome.java HelloWorld.java HelloWorldBean.java Compile Successfully After the COM / Javausr / Example Directory in the current directory. B, select [File] -> [New] -> [Enterprisebean] (or new EnterpriseBean shortcut on the direct selection window) will appear as shown below:

Fill in HelloWorldeJB in Jar Display Name. Join in the CONTENTS below. c, select the Next button. Go to the next interface, configure this EJB as a Stateless Session bean, and specify a class that has been compiled for the corresponding part. The last result is as shown below:

D, I have never selected the next button until the Security configuration window appears, as shown below:

In this, select Deployment Settings, pop up the dialog window in the middle of the image, and select the Support Client Choice option, then OK. At this time we can choose the finish button. This way our new EJB is created. Step 2: Release this application, that is, the EJB in this application is released. 1. Select [Tools] -> [Deploy] or deploy shortcut in the window, the following window will pop up: Select Return Client JAR in it, and the name of the file will be generated by default. This JAR file includes some classes for the STAND Alone Client and J2EE Application Client to access EJBs for remote access. Choose Next, go to the next step. 2, this step we have to specify the JNDI name for EJB so that the client can access it. As shown in the figure below, we entered "HelloWorld" as the JNDI name.

Now select the finish button, our app can be published on the specified server, and now you can access our EJB through the client. Appendix 3 - Creating a J2EE Application Client Component J2EE Application Client is also one of the J2EE components. We have to run it to add a J2EE Application Client component in J2EE SDK 1.3. Step 1: In the application created in the previous appendix, join the J2EE Application Client component. 1. Compile our source code, use the following instructions: d: /j2sdkee1.3/myapp/hellow ~ 1> javac -classpath.; D: /j2sdkee1.3/lib/j2ee.jar myj2eeppclient.java2, start J2EE server J2EE -verbose3, start the publishing tool deploytool. Deploytool4, select [File] -> [New] -> [Application Client Compoent] or shortcuts for New Application Client Compoent. 5. Skip the introduction page, join our compile .class file in Jar File Contents, as shown in the figure:

Also choose a good application for J2EE Application Client to be attributed. 6, select Next, in the following interface, the display of our J2EE Application Client is J2EEAppClient as shown below:

7. Continue NEXT Skip the Environment Entries window to the Enterprise Bean References window, define another JNDI reference for the HelloWorld EJB created in this window. Use the "EJB / HAI" JNDI name reference in my J2EE Application Client to access the HelloWorld EJB we have published. As shown below:

In Home Interface and Local / Remote Interface items, com.javausr.example.HelloWorldHome and com.javausr.example.HelloWorld, and specify the original JNDI name of EJB we want to refer to the deployment settings for ejb / hai below " HelloWorld. You can now choose Finish. Step 2: Publish a service, here we have to release applications that have already included J2EE Application Client components. Select [Tools] -> [Deploy] or deploy in the window, the following window will pop up: Select Return Client JAR in it, and the name of the file will be generated by default. This JAR file includes some classes for the STAND Alone Client and J2EE Application Client to access EJBs for remote access.

转载请注明原文地址:https://www.9cbs.com/read-40092.html

New Post(0)