PHP
  Home arrow PHP arrow Page 7 - Using PHP with Java
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Using PHP with Java
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2002-04-03

    Table of Contents:
  • Using PHP with Java
  • Getting Started
  • Rank And File
  • A Custom Job
  • Passing The Parcel
  • An Exceptionally Clever Cow
  • Beanie Baby

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using PHP with Java - Beanie Baby


    (Page 7 of 7 )

    Finally, how about connecting PHP up to a Java Bean? Here's the Bean, a simple Celsius-to-Fahrenheit-to-Celsius converter:

    temperature.zip

    The Bean exposes the following methods:

    getCelsius() - get the current value of the Celsius property

    getFahrenheit() - get the current value of the Fahrenheit property

    setCelsius(num) - set the current value of the Celsius property to num

    setFahrenheit(num) - set the current value of the Fahrenheit property to num

    convertCelsiusToFahrenheit(value) - convert Celsius value to Fahrenheit

    convertFahrenheitToCelsius(value) - convert Fahrenheit value to Celsius

    And here's a PHP script which combines user input with the Temperature Bean to perform temperature conversion:

    <html> <head><basefont face="Arial"></head> <body> <h2>Temperature Converter</h2> <?php if($submit) { // data type conversion setType($temp,"integer"); $myClass = new Java("Temperature"); if($units == "celsius" && $temp != "") { // use the Celsius functions $myClass->setCelsius($temp); // print result echo $myClass->getCelsius()," <sup>o</sup> Celsius is ",$myClass->convertCelsiusToFahrenheit($myClass->getCelsius())," <sup>o</sup> Fahrenheit."; } else if($units == "fahrenheit" && $temp != "") { // use the Fahrenheit functions $myClass->setFahrenheit($temp); // print result echo $myClass->getFahrenheit() , " <sup>o</sup> Fahrenheit is ", $myClass->convertFahrenheitToCelsius($myClass->getFahrenheit()) , " <sup>o</sup> Celsius."; } else { echo "Please enter a valid temperature and scale"; } } else { ?> <form action="<? echo $PHP_SELF; ?>" method="post"> <input type ="text" name="temp" size="4" maxlength="4"> <select name="units"> <option value="celsius">Celsius</option> <option value="fahrenheit">Fahrenheit</option> </select> <input type ="submit" name="submit" value="Convert"> </form> <? } ?> </body> </html>
    This script consists of two parts: the form which allows the user to select a temperature scale and enter a tempterature value, and the form processor which actually uses the Bean to perform the conversion and display the result.

    Here's what the form looks like:



    Once this form has been submitted, an object is instantiated from the Temperature class, and the information provided by the user is used to perform temperature conversion using the Bean methods described above. The result is then displayed to the user.

    Here's what the result looks like:



    Note that it's necessary to convert the type of the form variable $temp from string to integer in order to make it compatible with the arguments expected by the Bean - this data type conversion is one of the important issues you will face when accessing Java classes through PHP.

    And that's about it for the moment. In case you'd like to learn more, take a look at the following links:

    The PHP manual's Java pages, at http://www.php.net/manual/en/ref.java.php

    O'Reilly's PHP and Java tutorial on O'Reilly ONLamp.com, at http://www.onlamp.com/pub/a/php/2001/06/14/php_jav.html

    PHPBuilder's PHP and Java tutorial, at http://www.phpbuilder.com/columns/marknold20001221.php3

    See you soon! Note: All examples in this article have been tested on Linux/i586 with JDK 1.3.0, Apache 1.3.20 and PHP 4.1.1. Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    PHP ARTICLES

    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT