This post will build off of the classic Servlet/JSP MVC examples, as seen in tutorials like this. But instead of using a JSP to view the model, we will use PHP. So, really this just boils down to how you can pass a Java object to PHP.
Lets start with a simple servlet that contains this code in the process request:
request.setAttribute("message", "hello from java to php");
request.setAttribute("number", 100);
RequestDispatcher dispatcher;
dispatcher=request.getRequestDispatcher("/view.php");Ok, this code should be familiar, we simply place two objects inside the request and the forward the request dispatcher to a PHP page called 'vew.php' (instead of a jsp page). Now lets look at the PHP code.
dispatcher.forward(request, response);
The first thing we want to do in view.php is grab the Java request object, so we can grab the objects we placed in the request. Quercus provides the PHP function quercus_servlet_request() to do just this. Then I add a function to make it easier to get the object attributes out of the request. It is a good idea to put this code in a global include:
<?php
$request = quercus_servlet_request();
function getAttribute($key) {
global $request;
return $request->getAttribute($key);
}
?>
Now, within PHP we can call the above function to get the Java objects, and use PHP as the view component of the MVC. Piece of cake!
<?php
$message = getAttribute("message");
echo "message = ".$message."<br>";
$number = getAttribute("number");
echo "number = ".$number;
?>
No comments:
Post a Comment