Java Servlets & PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Sinnix
Forum Commoner
Posts: 43
Joined: Mon Apr 28, 2003 11:01 am
Location: Ottawa, ON Canada
Contact:

Java Servlets & PHP

Post by Sinnix »

I'm having severe issues getting my website to retrieve information from a Java Servlet server thingy. I need this:http://206.222.76.240:8080/LMS/intermap ... bmit+Query saved into a PHP variable so I can parse through it. Does anyone have any ideas?
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

have you tried grabbong the request string?
using sessions?
Sinnix
Forum Commoner
Posts: 43
Joined: Mon Apr 28, 2003 11:01 am
Location: Ottawa, ON Canada
Contact:

Post by Sinnix »

No I have not actually. I wasn't even aware that was possible. 8O I'll head to php.net and see what I can learn.
Sinnix
Forum Commoner
Posts: 43
Joined: Mon Apr 28, 2003 11:01 am
Location: Ottawa, ON Canada
Contact:

Post by Sinnix »

Alright... I checked it out... it's official. I have no idea what you're talking about. ;)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Just as a brief explanation:

Code: Select all

Test Link
Array
(
    [user] => bob
    [role] => student
    [request] => 
	 
		
			/MANIFEST[@course_id='1129']
		
	

		
    [Send_Request] => Submit Query
)

<pre>
<a href="index.php?user=bob&role=student&request=%3CLMSIS_REQUEST+request_type%3D%22get%22+content_type%3D%22manifest%22%3E%0D%0A%09%3CGET%3E+%0D%0A%09%09%3CXPATH%3E%0D%0A%09%09%09%2FMANIFEST%5B@course_id%3D%271129%27%5D%0D%0A%09%09%3C%2FXPATH%3E%0D%0A%09%3C%2FGET%3E%0D%0A%3C%2FLMSIS_REQUEST%3E%0D%0A%09%09&Send+Request=Submit+Query">Test Link</a>
<?php 
print_r($_GET);
show_source(__FILE__);
?>
Okay, so it needs rewriting (to get all the funny codes right), but the general usage of the $_GET was shown. You can also use $_SERVER['QUERY_STRING'] to get everything after the first '?' and go from there.

Hope it helped some.
Sinnix
Forum Commoner
Posts: 43
Joined: Mon Apr 28, 2003 11:01 am
Location: Ottawa, ON Canada
Contact:

Post by Sinnix »

Hmm... tried this out. Still not what I'm looking for I don't think ($_GET doesn't seem to be holding any data). Allow me to further clarify my problem.

My webserver is running on a seperate machine than the java servlet computer. What I'm needing to do is ...

index.html - user inputs an ID number, calls action.php

action.php - connects to java servlet server and requests XML file pertaining to previously entered ID #(see link above). action.php then saves the XML file into a variable. action.php then outputs specific information to the user, but not the entire file.

I can handle navigating the XML file, but I'm used to working with files directly... not getting feedback through a java servlet thing. :(
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

hmmm. not sure hot to have them interact.

i think it was 4.0.6 that was the last one with $HTTP_GET_VARS and after it was $_GET

check on your php version. knowing that will help us help you


ummm... hmmm

ssince it's two machines i'm not quite sure what to say.. not sure how much of an impact that will be... is php a cgi module or stand alone?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Sinnix wrote:Hmm... tried this out. Still not what I'm looking for I don't think ($_GET doesn't seem to be holding any data). Allow me to further clarify my problem.
Well, its because my example is printing out the value(s) to a page, and it contains controlcodes that makes certain things 'invisible'. Reason of gap in the print_r() array.

I'm not sure what information you need with the applet, but if you can clean up the URI from %22 and its likes, you will get a brand new result.
Sinnix
Forum Commoner
Posts: 43
Joined: Mon Apr 28, 2003 11:01 am
Location: Ottawa, ON Canada
Contact:

Post by Sinnix »

Yeah, I'm pretty much screwed. ;) I'm using the latest version of php too. Thanks for your help JAM, I'm gonna contact the makers of this damn servlet (all ASP/Java people) and ask them what I'm supposed to do with this crap. :evil:

Oh, and for the record... I freakin' hate Java. :wink:
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Haha ;)
Where did you get the applet from? Most applets (or any script-like code for that matter) usually comes with a piece of explanation and what usage/params that is needed.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

Sinnix wrote:Yeah, I'm pretty much screwed. ;) I'm using the latest version of php too. Thanks for your help JAM, I'm gonna contact the makers of this damn servlet (all ASP/Java people) and ask them what I'm supposed to do with this crap. :evil:

Oh, and for the record... I freakin' hate Java. :wink:
you could re-work it to use a db like say mysql instead of the servlets.. well i don't know for sure, but that's something to think about
Sinnix
Forum Commoner
Posts: 43
Joined: Mon Apr 28, 2003 11:01 am
Location: Ottawa, ON Canada
Contact:

Post by Sinnix »

Yeah, to answer both these questions... the servlet isn't mine. My company is doing a contract with another company and we're both responsible for different parts. It's the other company's stupid Java servlet that's causing the problems. I requested it in a database when the project started but, in the end, I got stuck with stupid servlets and XML files. :evil:

I called their project manager earlier today and told him that I was stuck, he said that he would have one of his coders send me a code snippit to parse out the XML by the end of the day. I'm *very much* looking forward to seeing this 'wonder code'. :? I'll be sure to post it if it works.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

:idea: I just happen to spot some examples in the php manual, that surely must help out. ;)
Try and see if you can work something out with this small example.

Code: Select all

<pre>
<?php
// this is your previously posted link
$link = "http://206.222.76.240:8080/LMS/intermap/servlet/information?user=bob&role=student&request=%3CLMSIS_REQUEST+request_type%3D%22get%22+content_type%3D%22manifest%22%3E%0D%0A%09%3CGET%3E+%0D%0A%09%09%3CXPATH%3E%0D%0A%09%09%09%2FMANIFEST%5B@course_id%3D%271129%27%5D%0D%0A%09%09%3C%2FXPATH%3E%0D%0A%09%3C%2FGET%3E%0D%0A%3C%2FLMSIS_REQUEST%3E%0D%0A%09%09&Send+Request=Submit+Query";
$a = explode('&', $link);
$i = 0;
while ($i < count($a)) {
    $b = split('=', $a[$i]);
    echo 'Value for parameter ', htmlspecialchars(urldecode($b[0])),
         ' is ', htmlspecialchars(urldecode($b[1])), "<br />\n";
    $i++;
}
?>
Sinnix
Forum Commoner
Posts: 43
Joined: Mon Apr 28, 2003 11:01 am
Location: Ottawa, ON Canada
Contact:

Post by Sinnix »

I got some example code back from the other developer, but it's kinda WAY above my head! lol

Code: Select all

<?php
include("AdgaLmsisClient.php");

$requestXml = "<LMSIS_REQUEST request_type='get' content_type='manifest'>";
$requestXml .= "<GET>\n<XPATH>";
$requestXml .= "/MANIFEST[@course_id='1131']";
$requestXml .= "</XPATH></GET></LMSIS_REQUEST>";

$client = new AdgaLmsisClient(
	"http://206.222.76.240:8080/LMS/intermap/servlet/information","rwolf");

//$responseXml = $client->getServerResponse($requestXml);
//echo "The response XML (whole response) was:\n";
//echo $responseXml;

echo "<html><body>";
$response = $client->handleRequest($requestXml);

if( $response['status'] == 'success' )
{
	// we have a successful response
	echo "<p>Request succeeded";
	echo "<ul>";
	foreach ($response as $responseKey=>$responseVal)
	{
		echo "<li>$responseKey: $responseVal";
	}
	echo "</ul>";
	
	// now we parse the inner response with the same parser as we used for the
	// outer response; this is not recommended (you should use another set of
	// parser handlers to avoid confusing code...); this is for purposes of example
	// I only partially parse the manifest here as a demonstration; a proper parser should
	// create a recursive data structure to hold any response in the general sense
	$innerResponse = $client->parseServerResponse($response['successInfo']);
	echo "<p>Manifest title:" . $innerResponse['courseTitle'];
}
else
{
	echo "<p>Request failed";
}
echo "</body></html>";

?>
and the second file...

Code: Select all

<?php
class AdgaLmsisClient
{
	var $serverBaseUrl;
	var $user;
	var $role;
	var $currentElement;
	
	function AdgaLmsisClient($serverBaseUrl,$user)	
	{
		$this->serverBaseUrl = $serverBaseUrl;
		$this->role = "trainer";
		$this->user = $user;
	}
	
	function handleRequest($xmlString)
	{
		$responseString = $this->getServerResponse($xmlString);
		return $this->parseServerResponse($responseString);
	}
	
	function getServerResponse($xmlString)
	{
		$url = $this->serverBaseUrl . "?user=".$this->user;
		$url .= "&role=".$this->role."&request=".urlencode($xmlString);
		
		// echo "<p>Using url:" . $url;
		$responseFH = fopen($url,"r");
		$responseString = "";
		while( $nextLine = fgets($responseFH,1024) )
		{
			$responseString .= $nextLine;
		}
		return $responseString;
	}
	
	function parseServerResponse($responseString)
	{
		$this->response = array(
			'requestType'=>'',
			'contentType'=>'',
			'status'=>'',
			'successInfo'=>"");
		$parser = xml_parser_create();
		xml_set_object($parser,$this);
		xml_set_element_handler($parser,"xmlStartElementHandler","xmlEndElementHandlder");
		xml_set_character_data_handler($parser,"xmlCdataHandler");
		xml_parse($parser,$responseString);
		return $this->response;
	}
	
	function xmlStartElementHandler($parser, $elementName, &$elementAttributes) 
	{
		// echo "<p>Saw $elementName";
		if( $elementName == "LMSIS_RESPONSE" )
		{
			$this->currentElement = $elementName;
			$this->response["status"] = $elementAttributes["STATUS"];
			$this->response["contentType"] = $elementAttributes["CONTENT_TYPE"];
			$this->response["requestType"] = $elementAttributes["REQUEST_TYPE"];
		}
		elseif( $elementName == "SUCCESS_INFO" )
		{
			$this->currentElement = $elementName;
		}
		elseif( $elementName == "MANIFEST" )
		{
			$this->response["courseTitle"] = $elementAttributes["TITLE"];
		}
	}
	
	function xmlEndElementHandlder($parser,$elementName)
	{
	}
	
	function xmlCdataHandler($parser,$cdata)
	{
		// echo "<br>$cdata";
		// this collects the inner XML from a get response
		if( $this->currentElement == "SUCCESS_INFO" )
		{
			$this->response["successInfo"] .= $cdata;
		}
	}
}

?>
:lol: I'm so screwed!! But... there it is... and it works! (although how is beyond me!)
Sinnix
Forum Commoner
Posts: 43
Joined: Mon Apr 28, 2003 11:01 am
Location: Ottawa, ON Canada
Contact:

Post by Sinnix »

I don't suppose someone could take a moment to explain a few things to me...

what exactly does this mean:

Code: Select all

$var1 = $var2->function($var3);
I mean, what's up with that "->" thing? And I've seen "=>" too, what's the difference? 8O
Post Reply