ServersJava Support Page 7

Java Support Page 7




Java support represents
without a doubt one of the new features that will open PHP up to existing
enterprise systems and applications. With PHP4, users can
instantiate and use Java servlets or classes.

Contents
Introduction
Differences Between PHP3 and PHP4
Output Buffering
Evaluate for Identical Operator
COM support on Windows
Displaying Portions of Raw HTML Code
Java Support
License
Conclusion

This support is not native
to PHP4; it necessitates the addition and configuration of an extension.
To have this extension at your disposition on a Linux system, it
is PHP4 must be recompiled by specifying the adequate configuration
options. These extensions are available in the form of objects that have
been precompiled for the Windows environment. These extensions can be
downloaded from the official PHP Web site. You must choose the extension
adapted to the Java version that you use (JDK 1.1.8, 1.2 or 1.3).
In the example, we are using a simple JDK 1.1.8.

In the php.ini
configuration file, you must add two lines in a new “Java” section as in
the following example:

[java]
java.class.path=”c:jdk1.1.8libclasses.zip;
C:Program
FilesApachecgi-binphp_java.jar;
C:Program
FilesApachehtdocsphpjava” extension=php_java.dll

Place the Java classes that you would like to use in
one of the directories given in the list of paths specified by the
java.class.path configuration directive, for example in C:Program
FilesApachehtdocsphpjava

Imagine that you have a Java class named Point, which
represents a point defined by its coordinates and the various
required methods (a constructor, a default constructor, traditional
access methods to set and get proprieties, and a toString method).
The Point class might look something like the following:

public class
Point {

//
attributes
public int abscissa;
public int ordinate;

public int
type;

// default
constructor
public Point()
{
  setCoordinates(0,0);
  this.type
= 1 ;
}
// constructor
public Point(int x,int y)
{
   setCoordinates(x,y);
  
this.type = 1 ;
}
// Overloaded
object method
public toString String(){
   //
Call the display method
   return
display();
}
// Point class methods
public void
setType(int mytype){
   // Call the display
method
   this.type = mytype;
}
public int
getAbscissa() { return abscissa; }
public int getOrdinate()
{ return ordinate; }
public void setCoordinates(int
abscissa, int ordinate) {   this.abscissa =
abscissa;
  this.ordinate = ordinate;
}
public sayHello
String() {
  return “I am a point :-)”;
}
public
display String(){
return “(“+abscissa+”,”+ordinate+” Type
“+type+”)”;
}
}

Thus, as long as Java support is configured correctly and the
bytecode file Point.class is in the appropriate place, we can
execute the following PHP code:

<?php

$myPoint = new
Java(“Point”);
echo “
Point created at (0,0)
…”;

echo
$myPoint->setCoordinates(15,15);
echo “
Point
moved to (15,15)”;
echo

“.$myPoint->display();
echo

“.$myPoint->sayHello();
echo


n”;


?>

Which triggers the
following display:

Point created at (0,0) …
Point moved to
(15,15)
(15,15 Type 1)
I am a Point 🙂

Even more interesting is
that a PointBis class can be derived from a Point class by overloading its
constructors.

public class
PointBis extends Point {

//
constructors
public PointBis() {
 
setCoordinates(0,0);

super.setType(2);
}
public PointBis(int x,int y)
{
  setCoordinates(x,y);

 
super.setType(2);
}

//
methods
public sayHello String() {
  return “I am a
PointBis ;^)”;
 }
}

In the same way, we can
instantiate and use an object of the PointBis class as in the following
example:

$myPointBis
= new Java(“PointBis”);
echo “
Point created at
(0,0) …”;

echo
$myPointBis->setCoordinates(17,21);
echo

Point moved to (17,21)”;
echo

“.$myPointBis->display ();
echo

“.$myPointBis->sayHello();

Which provides the following results:

Point
created at (0,0) …
Point moved to (17,21)
(17,21 Type
2)
I am a PointBis ;^)

But you might also want to
transfer objects to methods of other objects. For example, let’s take a
class that represents a vector. The vector is defined by its origin (a
point), its standard and its direction. In Java, the Vector class can be
defined in the following manner:

public class
Vector {

// attributes
private Point
origin;
private int standard;
private int
direction;

// constructors
public Vector() {
  Point p1
= new Point();
  setProps(p1, 0, 0);
}
public
Vector(Point orig, int s, int d) {
setProps(orig, s,
d);
}

// Overloaded Object method
public
toString() String{
  // Call the display
method
  return display ();
}
// Vector class
methods
public Point getOrigin () { return origin;
}
public int getStandard() { return standard; }
public
int getDirection() { return direction; }
public void
setOrigin (Point origin) {
  this.origin =
origin;
} ;
public void setStandard(int standard)
{
  this.standard =standard;
} ;
public void
setDirection(int direction) {
  this.direction =
direction;
} ;
public void setProps(Point origin, int
standard, int direction) {
  this.origin =
origin;
  this.standard = standard;
 
this.direction = direction;
}
public display()
String {
  return “(
“+origin.display()+
      ” /
“+standard+” / “+direction+” )”;
 
}
}

In PHP, we can instantiate
a vector by using the default constructor with the following
code:

$myVector = new Java(“Vector”);
echo

Vector created, default constructor…”;
echo

“.$myVector->display().”

n
n”;

 }
}

Which results in the
following display:

Vector created, default constructor…
( (0,0 Type
1) / 0 / 0 )

But we can also use the
Vector class constructor to transfer a PHP object
representing a Point object. Consider the example below

// creation
of a point that will serve as vector origin $myPoint = new
Java(“Point”, 11, 65);
echo “
Point
position…”;
echo

“.$myPoint->display().

n
n”;


$myVector =
new Java(“Vector”);
echo

“.$myVector->display().”

n
n”;

print
$myVector->setProps($myPoint, 24, 8);
echo

myVector’s origin is MyPoint”;

echo

Vector Position…”;
echo

“.$myVector->display().”

n
n”;

This code triggers the following display:

Point
Position…
(11,65 Type 1)
( (0,0 Type 1) / 0 / 0
)

myVector’s
origin is MyPoint
Vector Position…
( (11,65 Type 1) /
24 / 8 )

We can see that the vector
was originally created by its default constructor: It is a null vector for
which the origin is (0,0), having a null standard and a null angle
direction. Then, after calling the setProps() method, we make sure that
the vector origin corresponds to the coordinates of our myPoint point
(11,65).

This simple example is
a way of verifying whether Java can be used from PHP, regardless of if they are
your own Java codes or existing Java classes. This Java gateway has many
applications. Examples include, using CORBA components via IIOP, distributing processes via IIOP, distributing processes via RMI, and coding portions of “sensitive” code
of Java applications so that the source code cannot be unveiled.

Latest Posts

Related Stories