Unfair
Combined with J2EE and CORBA can make full use of the advantages of both to establish a better function. For example, the C implementation CORBA object is used as the service processing component to form a high performance business logic layer, and access the CORBA component in JSP. Floor. Let's take a look at an example of accessing a CORBA service object in JSP.
Software selection:
CORBA Service, we choose Zhongchuang Software Commercial Middleware Co., Ltd. (
INFORBUS of http://www.inforbus.com) (a distributed object middleware that follows CORBA specification), based on C is developed. Operating system selection IBM AIX (of course, you can also choose HP UNIX, Linux, or Windows).
CORBA client, in order to showcase the interoperability of CORBA, the ORB included in the JDK is used here. Of course, as a better choice, you can also use the INFORBUS's Java version to implement CORBA's client.
JSP Operation Environment, we choose COSCO Software Commercial Middleware Co., Ltd.
INFORWEB of http://www.inforbus.com (a application server that follows J2EE specification). The operating system selects Window (of course, you can also choose HP UNIX, Linux, or AIX).
Program implementation
Idl
IDL is a protocol for request calls for CORBA Services and clients. As long as you use the same IDL, client and service, you can perform seamless communication, and the development language of the client and the service party, the operating system has no relationship. Even you can choose a different CORBA middleware for the client and the server (as long as they follow the CORBA specification). When implementing the CORBA client program, you don't need to care about any details of the server, you need to care about only IDL!
Here we define the IDL shown below (file name Appteest.IDL):
Module Example {Interface A {Long Aoperation (in long ildata);
CORBA Services Program
The work that the CORBA service needs to do is mainly: map IDL to C , implement the interface defined in IDL (ie implementation processing logic), and write a main program instantiated service object.
1. Map IDL to C
This only needs to call the INFORBUS IDL compiler IDL, execute the following command:
IDL Apptest.IDL
This command will produce four files: Apptest.h, appteest.cpp, apptest_skel.cpp, apptest_skel.cpp, which contains Skeleton.
2. Implement the interface defined in IDL
Inherit Skeleton, implement the IDL interface definition, complete your business logic in the implementation class, in this example we implements a simple logic, multiply the received parameters by 2, then return the results, the code is as follows:
// File name: apptest_impl.h #ifndef APPTEST_IMPL_H #define APPTEST_IMPL_H #include "apptest_skel.h" class A_impl: public POA_example :: A, PortableServer :: RefCountServantBase {public: virtual CORBA :: Long AOperation (CORBA :: Long ilData) Throw (CORBA :: SystemException);}; #ENDIF / / / / File Name: Apptest_Impl.cpp // Contains CORBA System Class Library #include
In the main program, establish a CORBA environment, instantiate the service object and register the name service, then start waiting for the request, the code is as follows (for):
// / / file name: server.cpp #include
In the web application, it is mainly through a JSP to access the CORBA service object and showcase the Corba client. Implementing a web application requires two aspects: mapping the IDL to Java, sending requests in JSP and showing results.
1. Map IDL to Java
This only needs to call JDK IDL compiler IDLJ, execute the following command:
IDLJ Apptest.IDL
This command will produce five files: A. Java, AOperations.java, AHELPER.JAVA, AHOLDER.JAVA and _Astub.java include STUB.
2. Send a request and display the result in the JSP.
In order to avoid containing too many Script in JSP, the code to send the request is separated from JSP to a simple Java class.
The code of the Java class is as follows (ACLIENT.JAVA):
/ * * AClient.java * / package example; import org.omg.CORBA *;. Import org.omg.CORBA.ORBPackage.InvalidName; import org.omg.CosNaming *;. Import org.omg.CosNaming.NamingContextPackage.CannotProceed ; import org.omg.CosNaming.NamingContextPackage.NotFound; / ** * @author well fly * / public class AClient {public static int opA (int num) throws Exception {String [] args = { "-ORBInitRef", "NameService = corbaloc: iiop: 192.168.60.158: 900 / NameService "}; ORB orb = ORB.init (args, null); org.omg.CORBA.Object objRef; objRef = orb.resolve_initial_references (" NameService "); NamingContext ncRef = NamingContextHelper.narrow (Objref); NameComponent NC = New NameComponent ("a", "Operationa"); NameComponent Path [] = {nc}; org.omg.corba.object obja; obja = ncref.resolve (path); a A = AHELPER.NARROW (OBJA); int out; out = a.aopert:}}}
The code for the JSP file is as follows (INDEX.JSP):
<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ Page Import = "EXAMPLE.ACLIENT"%>
Ok, compile your program, run the name service, run the CORBA service, run the application server INFORWEB, deploy the web application, and then you can access it through the browser.