# Solved: Using java program from browser



## steven1350 (Jun 3, 2006)

I have created a java program which uses the Yahoo API to do searches for part of an assignment. The program consists of two programs, one uses the API, and creates an XML file. The other reads the XML file, and generates an HTML file.

Now, I need to be able for users to get to a website, and enter a query. When they press the submit button, I want it to call my java program, make the HTML file, and then the browser redirects to the HTML page.

Now what I tried to do was make a CGI file, or a PHP file, which does a system call the run the program with the query, but I couldn't get it to work. (running in a shell window did work )

Any suggestions on how I should get it to work?


----------



## jdean (Jan 20, 2002)

First of all, the way to get this to work is to figure out why it works in a shell but not with the system call. I would suggest adding some tracing/logging code to see what's happening. It might be a permissions problem when running the program or when writing the html file.

Second, while it's hard to know exactly what you're doing here, it's not clear why you would do a redirect. Just return the generated page directly to the user.

Third, doing a CGI to a Java program is going to be expensive. I hope you don't plan on doing this too often. Isn't it possible to do the whole thing in PHP?


----------



## steven1350 (Jun 3, 2006)

I would do it in php, but I have very little experience with PHP and XML 

And I wouldnt be doing it very often. Just for an assignment. After that, I will take the site down.

my php code looks like this, and it dont work 

<?php
print "Hi there
";
system('java CreateXML hello', $retval);
?>

That should pass the query Hello to the program, and the program will generate an XML file. But it wont work, nothing happens, no errors, nothing happens but a Hi there message


----------



## jdean (Jan 20, 2002)

When you make the system() call, how does java locate CreateXML?


----------



## steven1350 (Jun 3, 2006)

its in the same directory


----------



## jdean (Jan 20, 2002)

How do you know what directory system() is using?


----------



## Chicon (Jul 29, 2004)

steven1350 said:


> ...
> Now, I need to be able for users to get to a website, and enter a query. When they press the submit button, I want it to call my java program, make the HTML file, and then the browser redirects to the HTML page.
> ...
> Any suggestions on how I should get it to work?


Hi steven1350,

To do so, you need to learn about Java servlet technology. Indeed, servlets allow to build dynamically HTML pages and other documents.
Also, servlets must be hosted on servers that allow Java applications to run like Apache Tomcat, WebLogic, JBoss, GlassFish.


----------



## JimmySeal (Sep 25, 2007)

I think servlets would be the logical way to go, but you might be able to make this work with PHP.

Once the Java program generates the HTML, what does it do with it? You need a way of accessing it from your PHP code. If the Java program is outputting the file to the local directory you can load up its html by using the PHP include() function.
You'd probably want to add the ability to pass in a (unique) file name so that you don't run into naming collisions.


----------



## steven1350 (Jun 3, 2006)

lol, forgot to mark this thread as solved 

I did use PHP to get it working. Turns out the server I was originally using has tight security restrictions, and system calls (other then a select few) were disabled.



> Once the Java program generates the HTML, what does it do with it? You need a way of accessing it from your PHP code. If the Java program is outputting the file to the local directory you can load up its html by using the PHP include() function.
> You'd probably want to add the ability to pass in a (unique) file name so that you don't run into naming collisions.


I could, however, I just needed to be able to demo the website to an instructor. So there was never more then 1 user on the website, so unique naming was not neccessary. (It would always overwrite the same file)


----------

