Step by step teach you how to remotely call EJB

Source: Internet
Author: User

Step by step, you can remotely call ejbhttp: // www.diybl.com/course/?web/webjs/20071226/94785.htmlwww.diybl.com at: 2007-12-26 Author: anonymous name Edit: Click here: 686 [Comment]

Preparations: Find out how to use WebLogic (or JBoss), tomcat, jbluder (or eclipse + myeclipse), and write a simple Struts-based Web project, then prepare two connected computers (either LAN or LAN). If there are no conditions, you can install two Web servers (for example, Weblogic and tomcat) on the same computer, this article uses the latter method, Weblogic is used as the EJB container, Tomcat is used as the Web container, and the struts project is used to call the EJB

Are you ready? Let's start the next step.

Step 1: write a simple EJB. JBuilder is the best way to write ejbs. After all, it is silly. Of course, as a professional, eclipse and myeclipse are the best choice. However, as a beginner, JBuilder is recommended. The following is the EJB used in this article.

Remote interface:

Package testhello;

Import javax. EJB. ejbobject;

Public interface sayhello extends ejbobject {
Public String sayhello (string name) throws java. RMI. RemoteException;
}

Home interface:

Package testhello;

Import javax. EJB. ejbhome;
Import javax. EJB. createexception;
Import java. RMI. RemoteException;

Public interface sayhellohome extends ejbhome {
Public sayhello create () throws createexception, RemoteException;
}

Bean class:

Package testhello;

Import javax. EJB. sessionbean;
Import javax. EJB. sessioncontext;
Import javax. EJB. createexception;

Public class sayhellobean implements sessionbean {
Sessioncontext;
Public void ejbcreate () throws createexception {
}

Public void ejbremove (){
}

Public void ejbactivate (){
}

Public void ejbpassivate (){
}

Public void setsessioncontext (sessioncontext ){
This. sessioncontext = sessioncontext;
}

Public String sayhello (string name ){
Return "hello" + name;
}
}

If you use the above two tools for writing, you do not have to consider the configuration file.

Step 2: Use JBuilder or eclipse to compile and package the EJB project and obtain a jar file (If your project name is testhello, the JAR file is testhello. Jar. If your EJB container (WebLogic or JBoss) is on the local machine, you can right-click the EJB project in JBuilder or eclipse to deploy the EJB. To deploy the application to a remote server, you only need to upload testhello. jar to the remote end through the console of the EJB container, and then deploy the EJB in the EJB modler as prompted. Finally, do not forget to check the JNDI name of your EJB project in the JNDI tree. In this example, the JNDI name is sayhello.

Step 3: Package the remote interface and home interface into a jar file, copy to the lib directory under the struts project you want to remotely call EJB (for example: helloapp-> WEB-INF-> Lib)

Step 4: Set weblogic. jar (in the Weblogic installation directory-> weblogic81-> server-> lib folder) copy to the-> shared-> lib folder under the tomcat installation directory, in fact, all we need here is weblogic. only a few class files in jar, but for beginners, you don't have to go into the specifics of the class.

Step 5: compile a simple struts project (which can be generated using tools). The following code calls the helloaction of EJB.Source code(Note that you must import the jar package generated in step 3 to the editor. OtherwiseCodeCompilation and translation fail. If you do not know how to import the jar package, copy more of the jar package to your JDK installation directory-> JRE-> Lib-> ext folder)

Package logging. actions;
Import logging. constants;

Import Java. util. *;
Import javax. servlet. servletexception;
Import javax. servlet. requestdispatcher;
Import javax. servlet. HTTP. httpsession;
Import javax. servlet. HTTP. httpservletrequest;
Import javax. servlet. HTTP. httpservletresponse;
Import javax. naming. context;
Import javax. naming. initialcontext;
Import javax. naming. namingexception;
Import javax. RMI. portableremoteobject;

Import Org. apache. struts. action. action;
Import Org. apache. struts. action. actionform;
Import Org. apache. struts. action. actionforward;
Import Org. apache. struts. action. actionmapping;
Import Org. apache. struts. action. actionmessage;
Import Org. apache. struts. action. actionmessages;
Import Org. apache. struts. util. messageresources;
Import Org. apache. struts. validator. dynavalidatorform;

Public final class helloaction extends action {
 
Public actionforward execute (actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response)
Throws exception {

Initialcontext CTX = This. getinitialcontext ();

// Find the EJB component whose JNDI name is sayhello
Object OBJ = CTX. Lookup ("sayhello ");

// obtain the reference of the home interface of the remote EJB component
testhello. sayhellohome home = (testhello. sayhellohome) portableremoteobject. narrow (OBJ, testhello. sayhellohome. class);

// Obtain the reference of the remote interface of the remote EJB component
Testhello. sayhello Hello = home. Create ();

String name = "see the wind ";
String saystring = Hello. sayhello (name );

Request. setattribute ("username", name );
Request. setattribute ("password", saystring );
Request. removeattribute (mapping. getattribute ());

Return Mapping. findforward ("loginsuccess ");
}
 

/* The following method is used to select the network location of the JNDI driver and server by passing the environment attribute,
Connect to the JNDI tree.
This is the fixed initialization mode for remotely calling EJB when Weblogic is used as the EJB container. Beginners can note it down.
*/
Private initialcontext getinitialcontext () throws exception {
// Address of the EJB container
String url = "T3: // image: 7001 ";
String user = NULL;
String Password = NULL;
Properties properties;

Properties = new properties ();
Properties. Put (context. initial_context_factory,
"Weblogic. JNDI. wlinitialcontextfactory ");
Properties. Put (context. provider_url, URL );
If (user! = NULL ){
Properties. Put (context. security_principal, user );
Properties. Put (context. security_credentials,
Password = NULL? "": Password );
}
Return new javax. Naming. initialcontext (properties );
}
}

Step 6: if you follow the above steps strictly, start Weblogic and tomcat to test at the same time.

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.