Use SOAP to Access EJB Components with PHP

Use SOAP to Access EJB Components with PHP

magine a software project for which the front-end developers work with PHP and the back-end developers create Enterprise JavaBeans (EJBs). As the project evolves, the front-end applications need to speak to the back-end EJBs, but this can’t be done in one simple step. So what’s the solution? Well, exposing the EJBs as Web services enables PHP to work with them. This article provides just such a solution.

Many developers face the challenge of mixing Java and PHP, based on the numerous posts I see on different discussion forums asking, “How do I link PHP with EJB?!” The few answers they receive usually suggest using Web services, but they never offer a practical example. Because this problem has no easy solution, the practical example this article offers is complex, involving Java, PHP, and SOAP technologies. (IMHO, the process of exposing EJBs as Web services requires much more coverage than one article can offer.)

PHP developers who have tried to work with EJB know it is impossible unless the EJB components are exposed as Web services. In large organizations, these developers usually do not have the rights or permissions to manipulate existing EJBs or to create and deploy new ones. So, they need to request their “Application Deployer” managers to expose the EJBs. In smaller shops, some developers probably work with EJB as well as PHP. For those jacks of all trades, this article demonstrates how to:

  1. Configure an EJB component (a simple stateless session bean)
  2. Deploy it as a Web service with SOAP
  3. Modify their PHP code to access the EJB component

The following is a brief listing of all the technologies and software used in this article:

  • J2SE (version 1.4.1) to create EJB components
  • J2EE RI, which is in the J2EE package (version 1.3.1) as the application server
  • Apache SOAP (version 2.3.1) to take advantage of SOAP technology
  • Apache HTTP Server (version 1.3.31) to test the PHP code
  • PHP (version 4.3.8)
  • NuSOAP library for work with SOAP in PHP

Author’s Note: This article is not a detailed manual for installing and configuring PHP software, nor is it a manual for EJB component development. For more detailed information on these subjects, consult their official documentation or any of the numerous books about them. The only purpose of this article is to show the reader how to access an EJB component from PHP code. By the way, I used the Windows operation system, but since this software is all cross-platform, you should not have any problems running it on UNIX, for example.

Before jumping into the code, take a look at Figure 1, which shows a high-level theoretical diagram of the solution that you will implement.

Click to enlarge
Figure 1: Theoretical Diagram of Solution

Prepare Your EJB Component
First things first: make sure you’ve downloaded and properly installed J2SE and J2EE. You need them to follow the example EJB component.

The following are the source code listings for the stateless session bean:

  • Remote home interface: greentest/
    package greentest;import javax.ejb.*;import java.rmi.RemoteException;public interface HelloWorldHome extends EJBHome {	public HelloWorld create() throws CreateException, RemoteException;}
  • Remote interface: greentest/
    package greentest;import javax.ejb.*;import java.rmi.RemoteException;public interface HelloWorld extends EJBObject {	public String say() throws RemoteException;}
  • And the bean itself: greentest/
    package greentest;import javax.ejb.*;public class HelloWorldBean implements SessionBean {	public void setSessionContext(SessionContext ctx) {		System.out.println("In setSessionContext(...)");	}	public void ejbCreate() {		System.out.println("In ejbCreate()");	}public void ejbActivate() {		System.out.println("In ejbActivate()");	}	public void ejbPassivate() {		System.out.println("In ejbPassivate()");	}	public void ejbRemove() {		System.out.println("In ejbRemove()");	}	public String say() {		System.out.println("In business method say()");		return "Hello World! I am just tiny Stateless Session Bean!";	}}

As you can see, the only active business method in the bean is say(), which returns the “Hello World” string. So, as long as you’ve installed J2SE and J2EE and modified the CLASSPATH environment variable, you can compile everything now. All source files should be in directory src/, and all classes will need to be in directory classes/. Also, don’t forget that as long as the enterprise bean is in the package greentest, you need a subdirectory named greentest in classes/ and src/ as well. Other than that, the compilation process is easy:

cd srcjavac -d ..classes greentest*.java

You should not have any problems if you correctly followed all the steps and set CLASSPATH correctly.

Deploy Your EJB Component
Next, you need to deploy the component bean. So, run your J2EE RI server:

j2ee –verbose

The –verbose option allows you to see all the debug messages from the server.

Now, in a separate window (the J2EE RI server needs to be running all the time), run the deploy tool from J2EE package:


To prepare and deploy the EJB component, take the following steps (consult the screenshots for guidance):

    Click to enlarge
    Figure 2: New Application Wizard
  2. Click OK. Single click on AlohaApp.
    Click to enlarge
    Figure 3: New Enterprise Bean Wizard – EJB JAR
  4. Click NEXT
    Click to enlarge
    Figure 4: New Enterprise Bean Wizard – General
  5. Click NEXT, then FINISH.
    Click to enlarge
    Figure 5: Deploy AlohaApp – Introduction
  7. Click NEXT
    Click to enlarge
    Figure 6: Deploy AlohaApp – JNDI Names
  8. Click FINISH
    Click to enlarge
    Figure 7: Deployment Progress

If you arrived at the screen display above, you did everything correctly and your EJB component is deployed.

Test Your EJB Component
Now it’s time to test whether it works. You need to compile your client tool: The following is the source code:

import javax.ejb.*;import javax.naming.*;import java.rmi.*;import javax.rmi.*;import greentest.*;public class HelloWorldClient {	public void go() {		try {			Context ic = new InitialContext();			// during deployment, in JNDI we called it 'Aloha', remember?			Object o = ic.lookup("Aloha");			HelloWorldHome home = (HelloWorldHome) PortableRemoteObject.narrow(o,
HelloWorldHome.class); HelloWorld aloha = home.create(); System.out.println(aloha.say()); } catch (Exception ex) { ex.printStackTrace(); } } public static void main(String[] args) { new HelloWorldClient().go(); }}

To compile it, use AlohaAppClient.jar (consisting of classes and stubs), which the deployment tool generated during the steps above. The compilation process itself is pretty easy:

javac -classpath %CLASSPATH%;C:greenphp_with_ejbAlohaAppClient.jar

Execute the normal client for your stateless session bean.

Click to enlarge
Figure 8: Executed Client for Stateless Session Bean

If you see the above output, you’ve done everything correctly.

Install Apache SOAP
At this point, you need to download and install Apache SOAP and then integrate it into your application. Go back to your deployment tool window (Did you close it already? Too fast! Open it once again!) and take the following steps:

  1. Single click on AlohaApp.


    Click to enlarge
    Figure 9: Add Web WAR
  2. Click on ADD WEB WAR (choose soap.war in the webapps/ directory where your Apache SOAP is installed, like in the screenshot above).


    Click to enlarge
    Figure 10: Deploy AlohaApp – Introduction
  3. Click NEXT, again NEXT, then FINISH

    If you’ve done everything correctly, you will see the screen below.

    Click to enlarge
    Figure 11: Deployment Progress

Now, to be 100 percent sure that your Apache SOAP is installed and working properly, type this URL in your browser window: http://localhost:8000/admin/. If everything is okay, you will see the Apache SOAP Admin interface.

Have SOAP Expose Your EJB Component
Now, you need to tell Apache SOAP that you want to expose the EJB component to it. To accomplish this, use the next deployment descriptor, soap_dd.xml:

				scope="Application" methods="create">													value="com.sun.jndi.cosnaming.CNCtxFactory" />		org.apache.soap.server.DOMFaultListener

To deploy the EJB component as a Web service to Apache SOAP, use the following command:

java org.apache.soap.server.ServiceManagerClient 
http://localhost:8000/servlet/rpcrouter deploy soap_dd.xml

It’s that easy!

Create a PHP Client to Access Your Bean
The next goal is to create a PHP client for your stateless session bean. The client will communicate with the bean by using SOAP. Assuming you successfully downloaded, installed, and configured Apache HTTP Server and PHP to “see” each other, you just need to download NuSOAP. The only thing you need to do to install it is copy file nusoap.php into the directory where you put your simple PHP code (in this case, it is C:Program FilesApache GroupApachehtdocszzz). Put the code for the PHP client into the same directory, and call all the PHP material (– soap_client.php) with the following code:

call('say', array(), 'urn:demo');print_r($x);?>

Pretty simple, isn’t it? Now it’s show time! Just check that everything is as it should be:

  • The J2EE RI server is still running.
  • The Apache HTTP server is running (and configured properly to understand PHP code).
  • All the source code is where it should be.
  • Everything is properly deployed (you should not find any mistakes if you followed all steps above).

Open your browser and type the URL: http://localhost/zzz/soap_client.php. You should see the screenshot below.

Click to enlarge
Figure 12: Deployment Progress

Mission Accomplished
Congratulations! You were able to run your own PHP code, which successfully accessed your EJB component. I hope this solution will help you in the future. Feel free to contact me, if you will have any problems with the this example. I can help you either eliminate them or at least find your mistakes. Remember, while you have J2EE RI running, you can see all the debug messages. They’ll tell you what’s happening and where you have any problems. These messages are pretty clear, so they can be a great help.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist