Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java and PHP will work together!

JavaFAQ Home » Good To Know Go to all tips in Good To Know


Bookmark and Share

Look at Java PHP integration here! Examples included.

Good news for those who likes PHP as well as Java! In Java 6, J2EE (Java Enterprise edition) will be able to work with PHP and another scripting languages. Java and PHP will be intergated together. Java classes will be accessible from different scripting engines, for example from PHP. The Java classes will be a part of a Servlet application or will be in a standard Java VM.

The JavaTM Servlet specification (Servlet 2.4, JSR-154, is the latest version) defines the set of core abstractions that are used by Java developer writing Web Applications. These abstractions include those of Web Application context, session, request, response, etc.

When Java developers will write Web Applications, they will write classes that interact with these objects in well defined security, resource and class loader contexts.

A Java API that used, will possibly through JNI interact with an scripting language engine to access the desired Java objects. By now most of work is done for PHP (Zend Technologies, Ltd, is a part of team, developing the feature ), but it will be possible to work with script engine which will implement JSR 223.

It will be possible to bundle scripting pages into a WAR file, either stand-alone, or as part of an EAR, providing real Java PHP integration.

You can ask - What is difference from today? It is already possible to do Java PHP integration now... The difference is that it will be a part of Java installation and you do not need custom solutions and make it manualy...

It is possible to do Java PHP integration, look here:

1. Bridge (integrate) PHP into Java: you can either integrate PHP into a Java Servlet environment, which is the more stable and efficient solution. This provided by a SAPI module that interfaces with the Servlet server.

The Java Servlet SAPI enables the entire PHP processor to be run as a servlet. The primary advanatage of this from a PHP perspective is that web servers which support servlets typically take great care in pooling and reusing JVMs. Build instructions for the Servlet SAPI module can be found in php4/sapi/README. Notes:

  • While this code is intended to be able to run on any servlet engine, it has only been tested on Apache's Jakarta/tomcat to date. Bug reports, success stories and/or patches required to get this code to run on other engines would be appreciated.
  • PHP has a habit of changing the working directory. sapi/servlet will eventually change it back, but while PHP is running the servlet engine may not be able to load any classes from the CLASSPATH which are specified using a relative directory syntax, or find the work directory used for administration and JSP compilation tasks.

2. Or bridge (integrate) Java support into PHP. This can be done by this Java extension.

The Java extension provides a simple and effective means for creating and invoking methods on Java objects from PHP. The JVM is created using JNI, and everything runs in-process.

Requirements

You need a Java VM installed on your machine to use this extension.

Installation

Build instructions for this extension can be found in php4/ext/java/README.

Runtime Configuration

The php.ini settings involved in configuring the Java extension are java.library.path and java.class.path.

Examples

 

Java PHP working Example 1. Use Java from PHP Example

<?php
  // get instance of Java class java.lang.System in PHP
  $system = new Java('java.lang.System');

  // demonstrate property access
  print 'Java version='.$system->getProperty('java.version').' <br>';
  print 'Java vendor=' .$system->getProperty('java.vendor').' <br>';
  print 'OS='.$system->getProperty('os.name').' '.
              $system->getProperty('os.version').' on '.
              $system->getProperty('os.arch').' <br>';

  // java.util.Date example
  $formatter = new Java('java.text.SimpleDateFormat',
                        "EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");

  print $formatter->format(new Java('java.util.Date'));
?>

PHP Java working Example 2. Use Java from PHP: AWT Example

<?php
  // This example is only intented to be run as a CGI.

  $frame  = new Java('java.awt.Frame', 'PHP');
  $button = new Java('java.awt.Button', 'Hello Java World!');

  $frame->add('North', $button);
  $frame->validate();
  $frame->pack();
  $frame->visible = True;

  $thread = new Java('java.lang.Thread');
  $thread->sleep(10000);

  $frame->dispose();
?>
 

This tip is based on todays breaking news in Java and PHP world, php documentation from php.net site (examples).


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy