Advertisment

Using Java with PHP

author-image
PCQ Bureau
New Update

PHP is a server-side scripting language similar to ASP (Active Server Pages) and JSP (Java Server Pages). While PHP itself has many features to fulfill the dreams of a scripter, this feature of using Java with PHP promises to capture the attention of Java programmers and JSP scripters through the power of Java language. 

Advertisment

Usually, when scripting in PHP one uses some of its many pre-defined functions. For example, to validate entries in HTML forms, you may use the String functions of PHP. For a first-time scripter, this will require reading the PHP manuals to know the signature of the methods and experiment with them to know their correct usage. A Java programmer, however, can use the tried and trusted methods of the Java String class.The example code below will explain better. 

Set it up 

First, you need to install JDK (Java Development Kit). We used JDK 1.4.1, downloadble from http://java.sun.com. By default, this version gets installed in
c:\j2sdk1.4.1.

Next, set up the latest version of PHP (4.2.1) on IIS 5.1 on a Win XP machine. The steps to setting up PHP in Win 2000 and Win NT are similar. If you don’t have IIS, install it though Start>Control Panel>Add or Remove Programs. Go to
Add/Remove Windows Components and choose IIS. You will need your Win XP Pro CD for this.

Advertisment

We assume that Windows is installed in C:\. To install PHP, unzip the file php-4.2.1-Win32.zip from the ‘path on the CD’ directory on this month’s CD to c:\. This will create a directory named php-4.2.1-Win32 in C:\. Rename it as php. Copy the file ‘php.ini-recommended’ to c:\windows directory. Rename it as php.ini and open it in Notepad. In the section Paths and Directories, change the line ‘doc_root =’ to:

doc_root =“c:\inpetpub\wwwroot” 

and the line 

Advertisment

‘extension_dir=./’ 

to:

extension_dir = “c:\php\extensions”

Advertisment

In the section Dynamic Extensions, uncomment the following line

;extension=php_java.dll

by removing the semicolon. Next, scroll to the Java section and modify the lines as follows.

Advertisment

java.class.path = “c:\php\extensions\php_java.jar”

java.home = c:\j2sdk1.4.0

java.library = C:\j2sdk1.4.0\j- re\bin\client\jvm.dll

java.library.path = c:\php\extensions

Make sure you remove any preceding semicolons, which would otherwise comment out the line. Copy the file php4ts.dll in c:\php to c:\windows\system32 directory. Launch the Internet Information Services applet in Control Panel> Administrative Tools. Expand the tree by clicking on the plus (+) icon beside the computer name (labeled with a computer icon) and then on Web Sites. Right-click on Default Web Site and select Properties. Click on the ISAPI Filters tab and then on the Add button. For the filter name, enter php and for the executable enter the path C:\php\sapi\php4isapi.dll. Click on the Home Directory tab and then on the Configuration button. Now, click of the add button Under Mappings. For the executable, enter the path C:\php\sapi\php4isapi.dll and for the extension enter .php (note the dot prefix). When done, restart IIS by right clicking on the computer name and selecting All Tasks>Restart IIS. Remember to restart IIS after any change in php.ini.

See it work

Consider a simple HTML page with a Form text field where a person is supposed to enter his
name. 

Advertisment






Enter an integer: 







Advertisment

Type the above in Notepad and save the file as form.htm in the directory c:\inetpub\wwwroot–the document root directory of IIS. Pressing the submit button will invoke the check.php script. This ensures that something has been entered in the text field, ie, the user has not submitted a blank form. The PHP script (check.php) is as follows.


$entered_value = $HTTP_POST_VARS<‘visitors_name’>;

if ($entered_value == “”)

print(“Error: You did not enter anything in the text field”);

else

print(“Welcome $entered_value”);

?>

Save the above code as check.php in the wwwroot directory. In the first line, using the name attribute of
tag, store the value of the text field (labeled visitors_name) into a variable called entered_value. Using the if-else statements in PHP, check for what was entered in the text field. If nothing was entered, an error message is shown, else a welcome message suffixed with the name entered is shown. You can see this working by keying in the URL http://127.0.0.1/ form.htm in IE.
There is, however, a catch in the method above. If someone enters only spaces in the text field,the welcome message will show up. To get around this you must strip all the leading and trailing spaces in the variable entered_value and then check for whether it is an empty string. There is a method called trim( ) in the Java String class which does this and we use this Java method in the following script.


$entered_value = $HTTP_POST_VARS<‘visitors_name’>;

$string_obj = new Java(“java.lang.String”,$entered_value);

$trimmed_value = $string_obj->trim();

if ($trimmed_value == “”)

print(“Error: You did not enter anything in the text field”);

else

print(“Welcome $entered_value”);

?>

In the above code a string object is first created as:

$string_obj = new Java(“java.lang.String”,$entered_value);

The syntax of the above statement is:

PHP_variable = new Java(“”,);

Here the must be prefixed with the its full package name. The String class has a constructor, which takes in a string as its parameter. In this case, we supply the string typed in the HTML form’s text field as the constructor parameter. Note that the is optional and need not be used in case of constructors with no parameters or default constructors. For example, we can create an empty String object as:

$string_obj1 = new Java(“java.lang.String”);

Next, the Java object reference–or simply the object–is stored in the PHP variable string_obj. Subsequently, calling different methods of the object is achieved as:

PHP_variable->method_name

For example:

$string_obj->trim();

The trim() method returns a string with the leading and the trailing spaces stripped off. We store this returned value in a PHP variable trimmed_value as:

trimmed_value = $string_obj->trim();

The subsequent code then checks the variable trimmed_value for an empty string. 

Accessing your own classes

Suppose you write your own Java class — MyClass — and want to access its method — myMethod through PHP. For this, first of all, myMethod( ) must be a public method. You need to tell PHP where the class byte file (MyClass.class) is located. This is similar to defining classpath in Java. Suppose MyClass.class is located in c:\java directory, open the file php.ini in your c:\windows directory and add c:\java to java.class.path — in the Java section as:

java.class.path = “c:\php\extensions\php_java.jar;c:\java”

Note that the path c:\java is separated from the earlier path — c:\php\extensions\php_java.jar — by a semicolon. Similarly you can add more paths to your class files separating each by a semicolon. The double quotes are mandatory.Now within a PHP script, you can use MyClass as follows:

$my_obj = new Java(“MyClass”);

$my_obj ->myMethod( );

You can set a public instance variable as:

$my_obj->myVariable = ;

Passing of Java objects as arguments

Sometimes we may need to pass Java objects as arguments to Java methods. Consider the following PHP script:


$integer1 = new Java(“java.lang.Integer”,12);

$integer2 = new Java(“java.lang.Integer”,12);

print($integer1->compareTo($integer2));

?>

The compareTo( ) method of the Integer class takes in an Integer object as argument and returns zero if the two Integer objects have the same value — as in this case. Note that first we create the two Integer objects–integer1 and integer2–and pass integer2 as an argument to the compareTo( ) method of integer1.

Shekhar Govindarajan

Advertisment

Stay connected with us through our social media channels for the latest updates and news!

Follow us: