How to Run PHP in JSP/JAVA
By Partho, Gaea News NetworkWednesday, September 23, 2009
With Apache Tomcat developers can enable PHP only in one web application and run it globally for all applications under tomcat. Generally, PHP is said to have several orders of magnitude more open source code and projects. Those developers favoring the Resin application server have the ubiquitous of PHP. Implementing PHP in Java would benefit both the PHP users and pure Java users. The developers can use the Java EE 5 platform-compliant Web container in Web Server to build reliable and scalable Web applications that are highly demanded. Here’s a comprehensive article that shows you how to run PHP in Java as well as JSP/Servlet-PHP integration options.
Clearly, developers using PHP with Java would have a number of advantages. Let’s see how to use Quercus to add PHP functions with a Java module
Before we begin with the steps we assume that you are working with Resin 3.0.17. Ensure that your directory housing httpd.exe is /var/www/webapps/ROOT. This directory would be mentioned as $webApp here. Now follow the steps
Step 1: Create resin-web.xml and put it in $webApp/WEB-INF
<web-app xmlns="https://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
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: Now create com.caucho.quercus.QuercusModule and put it in $webApp/WEB-INF/classes/META-INF/services
example.HelloModule
Step 5: Create hello.php and place it in webapps/ROOT
<?php echo hello_test(”World”) ?>
In your browser type this
https://localhost:8080/hello.php
Although Quercus is available with the latest Resin, it can also be downloaded as a WAR file which can be deployed on Resin or other application Servers.
By running Quercus and Resin you can deploy PHP web applications such as WordPress or Joomla! To run PHP applications on Resin with Quercus you just need to unzip the application into the <INSTALL DIRECTORY>\webapps directory. Ensure that you have the right JDBC dirver installed in the <INSTALL DIRECTORY>\lib directory.
There is a list of PHP applications that can be run on Java with Quercus.
Options For JSP / Servlet - PHP Integration
There are four integration options for JSP/Servlet Integration
Multi-port integration
This is easiest procedure using that you can just run Apache on default port 80 and Tomcat on default port (8080). They can simultaneously run on the same machine with different ports without any hitch-ups.
Single port integration
In this case Apache and Tomcat runs on different ports. In Tomcat any application can be mapped to a path on Apache server.
Single Port Integration( or the Apache Tomcat Connector)
For a better integration solution than Apache mapping you can look for single port integration. It can be configured to run static html pages in Apache and dynamic JSP pages using Tomcat.
Running PHP 5.x in Tomcat 5.x
Let’s see how to run PHP 5.x on Windows using tomcat 4.x or 5.x
We assume that you have Apache Tomcat already installed in your machine. Let’s see how to run php 5.x using Apache Tomcat 5.x.
Steps 1: Download the latest php 5.x zip file. PHP 5.2.11 zip package
Step 2: Download the latest collection of PECL modules. You may use ()
Step 3: Unzip the PHP 5.x zip files in your preferred location, say c:\php
Step 4: Copy php.ini-dist, in c:\php, as php.ini
Step 5: Remove the semi-colon at the beginning in php.ini:
extension=php_java.dll
Step 6: Extract php5servlet.dll from pecl zip file to c:\php
Step 7: Install Tomcat and create a directory under webapps. Let’s name it sun
Step 8: Create a WEB-INF directory under sun
Step 9: Create lib directory under WEB-INF
Step 10: Create web.xml under WEB-INF with the contents:
Web Application 2.3//EN"
"https://java.sun.com/dtd/web-app_2_3.dtd">
php
net.php.servlet
php-formatter
net.php.formatter
php
*.php
php-formatter
*.phps
Step 10: Extract php5srvlt.jar and extract/unjar (jar xvf …) it under c:\
Step 11: Modify both the files reflect.properties and servlet.properties to change the line library=phpsrvlt to library=php5servlet and save them. This indicates the file name of the dll file which is loaded by the Java application to serve the requests. In my version the name of the dll was php5servlet.dll. Your mileage may vary. This has no connection with the name of the jar file which can be anything.
Step 12: Re-create the jar file
Step 13: Copy the jar file to WEB-INF\lib directory created earlier
Step 14: Add c:\php to your System or User Path in Windows enironment (Right-click and select Properties from My Computer)
Step 15: Create a file test.php under fun with the following code:
Step 16: Start Tomcat (Go to [Tomcat installation directory]\bin and type Tomcat)
Step 17: Open your browser and go to https://localhost:8080/fun/test.php
Once you are done you will get an informative screen with php version information and whole lot of details.
In all the above options you can exchange data between PHP and Tomcat using the database, files, invoking appropriate URL with GET/POST data etc. For a direct communication you can run PHP 5.x in Tomcat solution. To see which Apache Tomcat production is right for you against the Servlet/JSP specifications see this.
Source: Quercus
Tags: Apache Tomcat, JSP, JSP/Servlet, Open Source, Run PHP in java, Servlet, Web application
December 14, 2009: 4:10 am
For our class project my group built a web-based |
December 12, 2009: 9:37 am
For application developers, this can lead to a rather daunting challenge, particularly when two disparate technologies need to coexist within the same application. An example of this is side-by-side deployment of PHP-based content and JSP-based content within the same Web application. This 10-Minute Solution offers one approach for deploying PHP and JSP pages within the same Web context. |
Handsoff |
JCMV |
accessoires