|
From: <php...@li...> - 2008-09-06 19:11:50
|
Hi,
I'm having trouble calling PHP from a JSP in a web application I am
working on. I've been working on this for several hours now and have
run out of things to try. I've searched Google and the archives for
this mailing list, but have been unable to find a solution. I'm hoping
someone here can help me out.
First off, some background information about my setup. I am running on
Ubuntu 7.10 (Gusty Gibbon). I am using Tomcat 5.5 as my web server. I
am using Eclipse as my IDE. Java, Tomcat, and Eclipse have all been
installed from the Ubuntu package repositories. I am using version
5.2.2 of the PHP Java Bridge.
I have placed the following JARs in my WEB-INF/lib directory:
JavaBridge.jar
php-script.jar
php-servlet.jar
script-api.jar
I have placed the following lines in my WEB-INF/web.xml file:
<listener>
<listener-class>php.java.servlet.ContextLoaderListener</listener-class>
</listener>
I don't know if it matters or not, but I am also using version
2.0.11.2 of Apache Struts. The appropriate Struts JARs have also been
placed in the WEB-INF/lib directory and the WEB-INFO/web.xml file has
also been updated for Struts accordingly.
I have created a simple JSP with some basic HTML content (a variant of
"Hello World") and have placed the following code snippet inside the
JSP in an attempt to use PHP:
<%
out.print( "[Before]" );
javax.script.ScriptEngine e =
php.java.script.EngineFactory.getPhpScriptEngine( this,
application,
request,
response );
e.getContext().setWriter( out );
e.eval( "<?php echo \"Test\"; ?>" );
out.print( "[After]" );
%>
When I view this JSP in my web browser, I see "[Before][After]" with
nothing in between [Before] and [After], along with the rest of the
page contents. I am expecting the word "Test" to appear between
[Before] and [After].
I have tried various PHP snippets in the eval line, and none of them
work. Here are the other things I've tried:
e.eval( "<?php echo phpinfo(); ?>" );
e.eval( new java.io.FileReader( "/var/www/header.php" ));
I've checked the two Tomcat logs that are being written to,
'catalina.2008-09-06.log' and 'localhost.2008-09-06.log'. While I was
thrashing about with some earlier attempts at getting this to work, I
would see exceptions being logged in one or both of those log files,
as well as appearing in my browser when viewing the JSP. With this
current arrangement, nothing is being logged in those two log files
and I am not seeing an exception in the browser when viewing the JSP.
Without a stack trace or other error message, I am at a loss for how
to proceed with diagnosing my problem. Any help you could provide here
would be very much appreciated.
--
Jeff Weston
PGP public key available from http://pgp.mit.edu/
PGP Key ID: 0x14B456ED
|
|
From: <php...@li...> - 2008-09-08 08:09:07
|
Hi, > First off, some background information about my setup. I am > running on > Ubuntu 7.10 (Gusty Gibbon). I am using Tomcat 5.5 as my web > server. I the current Tomcat version is 6.x, so I assume that you use the version which came with debian. Please either use the standard version of Tomcat with a standard version of Java. Or please use the PHP/Java Bridge debian package available from sourceforge. We've recently tried to set up the binary file on a debian machine and run into the same problems. Basically the tomcat version shipped with the debian os is patched. It has additional features which forbit accessing /bin/sh or /usr/bin/php for example. You must edit the appropriate config file and switch off the provided policy. Regards, Jost Boekemeier __________________________________________________ Do You Yahoo!? Sie sind Spam leid? Yahoo! Mail verfügt über einen herausragenden Schutz gegen Massenmails. http://mail.yahoo.com |
|
From: <php...@li...> - 2008-09-08 16:52:09
|
Hi. Thank you for your reply. I am still having the same problem. I've tried a variety of things in response to your reply. On Mon, Sep 8, 2008 at 1:09 AM, <php...@li...> wrote: > the current Tomcat version is 6.x, so I assume that you use the > version which came with debian. I am using Ubuntu 7.10 and the Tomcat package available in Ubuntu's package repositories. > Please either use the standard version of Tomcat with a standard > version of Java. Or please use the PHP/Java Bridge debian package > available from sourceforge. I looked at the PHP/Java Bridge Debian package available from SourceForge and saw that it installs a PHP configuration file, 'java.ini', and a bunch of files to '/usr/lib/php5/20060613+lfs'. Are these needed for calling PHP from Java, or are they only needed if you call Java from PHP? On the off chance they are needed to call PHP from Java, I installed them and restarted Apache. This did not solve my problem. I also switched out the JAR PHP/Java Bridge JAR files I am using for the ones included in the Debian package available from SourceForge. This did not solve my problem either. > We've recently tried to set up the binary file on a debian machine > and run into the same problems. Basically the tomcat version shipped > with the debian os is patched. It has additional features which > forbit accessing /bin/sh or /usr/bin/php for example. > > You must edit the appropriate config file and switch off the > provided policy. I turned off the security manager for Tomcat completely. This did not work for me either. -- Jeff Weston PGP public key available from http://pgp.mit.edu/ PGP Key ID: 0x14B456ED |
|
From: <php...@li...> - 2008-09-08 18:53:34
|
Hi Jeff, > > You must edit the appropriate config file and switch > off the > > provided policy. > > I turned off the security manager for Tomcat completely. > This did not > work for me either. Do you get a valid JSR223 script engine manager from the factory? Do you receive a javax.script.ScriptEngine? And is the script engine indeed the php.java.bridge.PhpScriptEngine or is it some other implementation? Can you please start log4j (e.g. with: "java -jar log4j.jar" -- please use the log4j contained in the bridge binary download) and restart tomcat with the log level set to 4 (e.g. with: JAVA_OPTS="-Dphp.java.bridge.default_log_level=4" sh catalina.sh run) and check the log? Regards, Jost Boekemeier __________________________________________________ Do You Yahoo!? Sie sind Spam leid? Yahoo! Mail verfügt über einen herausragenden Schutz gegen Massenmails. http://mail.yahoo.com |
|
From: <php...@li...> - 2008-09-08 20:30:26
Attachments:
exception.txt
|
Hi, On Mon, Sep 8, 2008 at 11:53 AM, <php...@li...> wrote: > Do you get a valid JSR223 script engine manager from the factory? > > Do you receive a javax.script.ScriptEngine? And is the script engine > indeed the php.java.bridge.PhpScriptEngine or is it some other > implementation? I'm not sure how to check these things. I added a toString() call on the script engine and got the following: "php.java.script.PhpServletScriptEngine@2b7bd9". Based on what I see there, I'm guessing that the correct implementation is being used. > Can you please start log4j (e.g. with: "java -jar log4j.jar" -- > please use the log4j contained in the bridge binary download) and > restart tomcat with the log level set to 4 (e.g. with: > JAVA_OPTS="-Dphp.java.bridge.default_log_level=4" sh catalina.sh > run) and check the log? Okay, I got logging working. The first thing I saw was an exception: "java.io.IOException: Permission denied". The stack trace is fairly lengthy, so I've attached it as a separate file (exception.txt). I think I'm getting this exception because the PHP/Java Bridge is attempting to create a temp file in my web directory, but the Tomcat user does not have write access to that directory. I remember seeing a system call to attempt to create a file in this directory when I was using strace to try and track down this problem yesterday. For now, I have made my web directory writable by tomcat (using chmod 777). When I ran the JSP again, I no longer see the exception in my log, but I still am not getting any output from PHP. This is the only other log entry I see related to the PHP/Java Bridge: Time: 8:18:05 PM Priority: DEBUG Thread: http-8180-Processor23 NDC: null Category: php.java.bridge.JavaBridge Location: null Message: created new bridge: php.java.bridge.JavaBridge@d0005e Throwable: null -- Jeff Weston PGP public key available from http://pgp.mit.edu/ PGP Key ID: 0x14B456ED |
|
From: <php...@li...> - 2008-09-09 15:29:36
|
Hi, [please use a standard tomcat installation!] the exception means that the bridge could not create a PHP file. Please change the permissions so that the bridge can create PHP files within the tomcat document root. The result is that PHP parses and returns an empty file. The bridge creates the PHP files within the tomcat document root because some PHP versions have the habit to change the cwd to the location of the PHP file. Since one cannot get a servlet engine to listen on a port 80 anyway, a writeable tomcat document root is not a security problem. Use Apache or IIS to forward requests from port 80 to the servlet back end. Regards, Jost Boekemeier __________________________________________________ Do You Yahoo!? Sie sind Spam leid? Yahoo! Mail verfügt über einen herausragenden Schutz gegen Massenmails. http://mail.yahoo.com |
|
From: <php...@li...> - 2008-09-13 17:25:03
|
Hi, On Tue, Sep 9, 2008 at 8:29 AM, <php...@li...> wrote: > [please use a standard tomcat installation!] For a number of reasons, I would really prefer to use the version of Tomcat that comes with Ubuntu. I think you are hung up on me not using a standard Tomcat installation. The exception was caused by the application running out of my home directory instead of as a WAR file that has been exploded in Tomcat's webaps directory. > the exception means that the bridge could not create a PHP file. > Please change the permissions so that the bridge can create PHP > files within the tomcat document root. The result is that PHP parses > and returns an empty file. My previous email already indicated I solved this problem by making the web directory of my application writable by Tomcat. I've verified that the PHP/Java Bridge is able to write files there my building a custom version of php-script.jar that does not delete the temp file. I've taken some time to dig into the PHP/Java Bridge code to try and see what is going wrong. I'm a little concerned about how the PHP/Java Bridge goes about calling PHP from Java. I see it creating a PHP temp file in my application's web directory containing the script I intend to run and then making a HTTP request to get that file back. When does PHP get executed in this process? The HTTP request is going to go to Tomcat, which is not going to call PHP to handle the PHP script. Why not just call PHP directly from Java and pass in the PHP script on standard input? That way you won't need to create a temp file or issue a HTTP request to process the PHP script. -- Jeff Weston PGP public key available from http://pgp.mit.edu/ PGP Key ID: 0x14B456ED |
|
From: <php...@li...> - 2008-09-14 18:48:43
|
Hi, > For a number of reasons, I would really prefer to use the > version of > Tomcat that comes with Ubuntu. the debian version starts Java as tomcat/root but fails to set the environment correctly: user.dir, user.home still point to /root. This is because the debian people do a user switch before executing java, but passing it the environment from root(!). NO web application can work correctly with such a setting. Seriously, don't use the debian-patched tomcat installation. Regards, Jost Boekemeier __________________________________________________ Do You Yahoo!? Sie sind Spam leid? Yahoo! Mail verfügt über einen herausragenden Schutz gegen Massenmails. http://mail.yahoo.com |
|
From: <php...@li...> - 2008-09-14 19:00:36
|
Hi, > Why not just [...] pass in the PHP script on standard input? because PHP needs a real file within the current working dir. Recent versions of PHP cannot include PHP files from urls anyway, unless you set the allow_url_include PHP option. However, you can subclass the php/java/script/PhpServletScriptEngine.java to do what you want. Please see: http://php-java-bridge.cvs.sourceforge.net/php-java-bridge/php-java-bridge/server/php/java/script/PhpServletScriptEngine.java?view=markup You need to customize the protected Object eval(Reader reader, ScriptContext context, String name) throws ScriptException; method. Regards, Jost Boekemeier __________________________________________________ Do You Yahoo!? Sie sind Spam leid? Yahoo! Mail verfügt über einen herausragenden Schutz gegen Massenmails. http://mail.yahoo.com |
|
From: <php...@li...> - 2008-09-18 16:46:00
|
On Sun, Sep 14, 2008 at 12:00 PM, <php...@li...> wrote: >> Why not just [...] pass in the PHP script on standard input? > > because PHP needs a real file within the current working dir. Recent > versions of PHP cannot include PHP files from urls anyway, unless > you set the allow_url_include PHP option. > > However, you can subclass the php/java/script/PhpServletScriptEngine.java > to do what you want. Please see: > > http://php-java-bridge.cvs.sourceforge.net/php-java-bridge/php-java-bridge/server/php/java/script/PhpServletScriptEngine.java?view=markup > > You need to customize the > > protected Object eval(Reader reader, ScriptContext context, String name) throws ScriptException; > > method. Thank you for your assistance with this. I will look into doing that. -- Jeff Weston PGP public key available from http://pgp.mit.edu/ PGP Key ID: 0x14B456ED |
|
From: <php...@li...> - 2008-09-20 15:56:00
|
Hi Jeff, > > However, you can subclass the > php/java/script/PhpServletScriptEngine.java > Thank you for your assistance with this. I will look into > doing that. I have changed the code accordingly. Please see the updated jsr223.jsp example: http://php-java-bridge.cvs.sourceforge.net/php-java-bridge/php-java-bridge/examples/php%2Bjsp/jsp%2Bphp.jsp?view=markup&sortby=date script=EngineFactory.getPhpScript(path, new StringReader("<?php...?>")); In order to link pre-existing scripts with JSP, you can call getPhpScript with a single argument: script=EngineFactory.getPhpScript(path/To/Your/PhpScript/w/o/.php); This way the PHP scripts can be cached, and you don't have to create a fresh String- or FileReader for each JSP invocation. Regards, Jost Bökemeier __________________________________________________ Do You Yahoo!? Sie sind Spam leid? Yahoo! Mail verfügt über einen herausragenden Schutz gegen Massenmails. http://mail.yahoo.com |