Quercus: Java and PHP integration
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Tutorials

Hello World
Java Modules
JSON
Hello World
Tutorials
JSON

Find this tutorial in: /usr/local/resin/webapps/resin-doc/quercus/tutorial/module
Try the Tutorial

Adding PHP functions with a Java module.

  1. Files in this tutorial
  2. Introduction
  3. Step 1: Create resin-web.xml and place it in $webApp/WEB-INF
  4. Step 2: Create HelloModule.java and place it in $webApp/WEB-INF/classes/example
  5. Step 3: Create com.caucho.quercus.QuercusModule and place it in $webApp/WEB-INF/classes/META-INF/services
  6. Step 4: Create hello.php and place it in webapps/ROOT
  7. Conclusion

Files in this tutorial

hello.php The PHP Hello, World
WEB-INF/classes/example/HelloModule.java The Java module definition
WEB-INF/resin-web.xml web.xml configuration
WEB-INF/classes/META-INF/services/com.caucho.quercus.QuercusModule Adding the module

Introduction
This article shows how to use Quercus, Resin's PHP implementation, to create a module in java callable from a PHP page.

For purposes of this article, I assume that you are working with Resin 3.0.17 and that the directory housing httpd.exe is /var/www/webapps/ROOT. I will call this directory $webApp.

Step 1: Create resin-web.xml and place it in $webApp/WEB-INF

resin-web.xml
<web-app xmlns="http://caucho.com/ns/resin">
   <servlet servlet-name="resin-php"
               servlet-class="com.caucho.quercus.servlet.QuercusServlet"/>
 
   <servlet-mapping url-pattern="*.php" servlet-name="resin-php"/>	   
</web-app>

Step 2: Create HelloModule.java and place it in $webApp/WEB-INF/classes/example

HelloModule.java
package example;
 
import com.caucho.quercus.module.AbstractQuercusModule;
 
public class HelloModule extends AbstractQuercusModule {
   /*
   ** Notice the careful use of the naming
   ** convention hello_test.  This is done
   ** in order to prevent name collisions
   ** among different libraries.
   */
   public String hello_test(String name)
   {
     return "Hello, " + name;
   }
}

Step 3: Create com.caucho.quercus.QuercusModule and place it in $webApp/WEB-INF/classes/META-INF/services

com.caucho.quercus.QuercusModule
example.HelloModule

Step 4: Create hello.php and place it in webapps/ROOT

hello.php
<?php echo hello_test("World") ?>
In your favorite browser, type:

 http://localhost:8080/hello.php
You should see:

 Hello, World

Conclusion

It is fairly straight forward to create your own modules callable from within a Quercus/PHP page. The above tutorial takes through the steps to create the simple hello world application (without needing to "jar-up" your files).

If you want to change your module in any way, all you have to do is resave the ".java" file in the classes\example directory, and Resin will recompile it for you.

You do not need to restart your web app or Resin. It's just that simple.

Try the Tutorial


Hello World
Tutorials
JSON
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.