Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Using PHP language in Java/JSP files.

Thread view: 
cejohnson@gmail.com - 29 Nov 2005 19:31 GMT
OK...I inherited a Java based app that I assume is coded completely in
Java (isn't that what jsp files are?). I needed to make an addition to
it, so rather than learning Java (since I'm trying to phase away from
it) I am wanting to make additions in php (as I know php, this was the
best thought to me).

Here's my problem. The Java app (at least all the jsp files) are in the
tomcat directory, so that Java/tomcat interprets them. Unfortunately,
any php code that is dropped into the tomcat file is ignored by the php
parser and spit out as html code. As Html doesn't know php from a hole
in the ground, I get some neato errors.

So...what do I need to change (and where please) so that a) php is
parsed within the tomcat directory; b) I can make a call to a php
script outside the tomcat directory or c) copy, rename, and open the
renamed copy using Java (if this is my only option...)

for PHP in the jsp files I've been trying one of the following (with no
success)

<script language="php">blah blah blah</script>
and
<?php blah blah blah ?>

Thoughts?
Oliver Wong - 29 Nov 2005 20:32 GMT
> OK...I inherited a Java based app that I assume is coded completely in
> Java (isn't that what jsp files are?). I needed to make an addition to
[quoted text clipped - 7 lines]
> parser and spit out as html code. As Html doesn't know php from a hole
> in the ground, I get some neato errors.

   Question is poorly phrased which indicates a lack of understanding of
the fundamentals. I suspect that it is not that the PHP parser is ignoring
the PHP code, but rather the PHP parser is not being invoked to run on the
PHP code. Furthermore, I suspect that you are not getting "neato errors" due
to HTML not knowing about PHP, but that either the JSP processor is trying
to interpret your PHP code and thus giving compilation errors, or the PHP
code is being emitted as HTML, and thus the browser is unsure of how to
render the elements it is encountering.

   What you're trying to do is somewhat unusual (but it has been done
before), and tricky. Do you know how to set up a PHP server on your own? If
not, you better figure it out.

   You need to decided the order in which the processors run. PHP, then JSP
or JSP then PHP? The choices have far reaching implications, so you need to
consider your decision carefully. If it's PHP first then JSP, then your PHP
code can output JSP code which will then get run. If it's JSP then PHP, then
it's the other way around.

> So...what do I need to change (and where please) so that a) php is
> parsed within the tomcat directory; b) I can make a call to a php
[quoted text clipped - 9 lines]
>
> Thoughts?

   For a minimal risk solution, I recommend you learn minimal Java, and
then use something like Runtime.Exec() to manually invoke the PHP parser,
passing it your script, and then taking the output and doing something with
it.

   With this solution, you can do all the "hard" work in PHP, but you'll
still need to know some Java to get the two to talk to each other.

   - Oliver
zero - 29 Nov 2005 20:50 GMT
cejohnson@gmail.com wrote in news:1133292697.026613.187690
@g49g2000cwa.googlegroups.com:

> OK...I inherited a Java based app that I assume is coded completely in
> Java (isn't that what jsp files are?). I needed to make an addition to
[quoted text clipped - 21 lines]
>
> Thoughts?

Interesting problem.  I couldn't find anything on the web about using jsp
and php code in the same file.

Do you have tomcat set up to run php (not inside jsp files, just pure
php), and does it get parsed correctly?  I believe php runs under tomcat
through a servlet - ie the php servlet interprets the php code, and
outputs it as html.  jsp parsing happens much the same way.  If I
understood your post correctly, you want the jsp code to be parsed first,
and use the output in your php code?  If that is the case, I think you
will need a custom servlet.  This custom servlet would first call the
normal JspServlet, and then send the output to the php servlet for
further processing.

I don't know if this would actually work, but I think theoretically it
should.

Alternatively, and perhaps more realistically, it is possible to call
java code from php.  Note that this is quite different from parsing jsp
and then further processing it as php.  jsp *uses* java code, but is
itself not java.  You would need to translate the jsp code into php.  For
example, if the jsp code is something like this:

<% String version = System.getProperty("java.version"); %>
<html>
<body>
<h2>Java version <%= version %></h2>
</body>
</html>

you would translate that into

<?php
$system = new Java('java.lang.System');
$version = $system->getProperty('java.version');
?>
<html>
<body>
<h2>Java version <?php echo $version ?></h2>
</body>
</html>

This way you can write all your code in php, and you only have to read
java - not write it.  Of course, this solution may not be possible in
your particular situation.  But if it is, it would be the easiest way to
go.

Signature

Beware the False Authority Syndrome

Ben_ - 29 Nov 2005 20:53 GMT
A classical approach is to setup PHP and have kind of two web sites.

But if I understand correctly, you want to code JSP and PHP in the same page
or integrate PHP fragments in a JSP.

Not an expert on this topic. More knowlegeable people will have to confirm.
But I think JSR 223 is what you have in mind.

I don't know if implementations are available, but at least a reference
implementation should exist.

Also, I know that ColdFusion has a product that plugs into a J2EE Web
Container and can render PHP pages. It's a Servlet that will load the PHP
page and interpret it. For the developer, it feels just like a regular PHP
server. Difference is it runs in Java.

I have mitigated feelings however. It depends on what you have to do
exactly, but I'm unsure which of the two is easier: learn the basics of JSP
to achive your work or lean integrating PHP into JSP.
Ziggie - 29 Nov 2005 21:22 GMT
I'm just gonna answer everyone at once, sorry if this seems disjointed:

Thanks for the input.  I apologize that I know next to nothing about
Java and JSP and that I tend to be a little tongue-in-cheek.

PHP is installed and working properly.  I am able to run PHP code that
is not included within the jsp files and not placed within the tomcat
directory.  The application is complete and written in jsp (which is
different than Java, right?) and I'm just trying to add one feature
which is easy in PHP, but seems to be very difficult in jsp/java.  I
don't know how to make jsp output php code (though I would try and link
to a php file outside of tomcat, but I need a variable that jsp already
has to be passed to the php code (oy vey).

Right now tomcat is not running php.  I'm not sure what servlet i would
need to install.  The basics are:  I have a jsp application that needs
a feature added to it.  I am trying to add php to the jsp file, so that
the php is processed, then outpout to the jsp which processes it and
converts it to whatever the browser needs.  Rewriting this entire
application into PHP is on the long-term to do list, but is not what I
was looking to do right now.

Basically, a user is going to click on a link, and this link will, once
clicked, take a file (which they are viewing) copy it to a
subdirectory, rename the extension (from test.txt to test.doc for
example) and then display the renamed file onscreen.  If there is a
simple (or even a complicated, but doable) way to do this with jsp, I
am all ears.

Does anyone know any good online java/jsp tutorials?

Again, thanks!
Ben_ - 30 Nov 2005 09:12 GMT
AFAIK, you won't be able to plug the output of the JSP interpreter into the
PHP interpreter, and have "double" parsing if it's what you want.

Using a JSR 223 implementation or a ColdFusion-like product, you could
however code JSP and PHP in the same page, and have Java interpret JSP and
PHP, which would fit your need.

Now, if the enhancement you need to code is:
* list files in a directory
* let user select a file
* copy the selected file
* rename the copy

Then it's not that hard to write in Java / JSP, and using PHP just because
you know it may be counter-productive.

You need to use FileInputStream & FileOutputStream to read the file in
blocks of bytes and write the bytes into the new file.

Here is an example of writing a copy of a file:
http://www.rgagnon.com/javadetails/java-0064.html.

For tutorials, there are so many sources... Sun has one. Here is a start:
http://java.sun.com/products/jsp/docs.html.
Ziggie - 30 Nov 2005 13:30 GMT
If it's simple enough to do in jsp then I'm all for having a go at it.
I did a google search on how to do what I'm trying to do and it was
full of things I didn't quite understand (hence, my wanting to do this
in php because it's easier).  However, I'm going to try it based on the
rgagnon link and see what I come up with.

Thanks everyone!  I'll let you know how this turns out.
Mike Gaab - 30 Nov 2005 00:21 GMT
> OK...I inherited a Java based app that I assume is coded completely in
> Java (isn't that what jsp files are?). I needed to make an addition to
[quoted text clipped - 21 lines]
>
> Thoughts?

Might look at this.

http://php-java-bridge.sourceforge.net/

A quote from the link.

"Since PHP/Java Bridge version 3.0 it is also possible to use java as an
"execution environment" for php scripts, java code can allocate and invoke
php scripts from an external or internal pool. The script instances can be
allocated from an external HTTP (e.g.: Apache/IIS) pool, from a internal php
FastCGI pool or directly using the php CGI interface. Java 6 or an external
javax.script package is required to use this functionality, please see the
javax.script package for details. "

If it works let us know.

Mike


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.