Click to See Complete Forum and Search --> : Java Servlet to call a Perl script
trojantiger
11-13-2008, 10:04 PM
Hi Everyone,
I would like to create a Java Servlet (to run under Tomcat) that will call a Perl script that I have written.
note: my perl script produces some output, and makes an XML file every time it is run, creating new XML files each time.
I would like to have my java servlet call this perl script, and receive back its XML file. I am not sure if it is easier to run the program, then look for the file it has written, or if i should have the perl script print back the XML file as a string somehow.
Since the perl file already produces an .xml file, i think it would be easiest for the java program to invoke the perl script, wait until it is finished, and then open the .xml file (so i can convert the XML into JSON).
Any ideas of how to do this?
Khalid Ali
11-14-2008, 05:03 AM
I have not worked with pearl scripts but if you put ur script on the web server and when you point to it using a url
http://myurl.com/script.pl
and if it returns an xml file in your browser window, then you are in luck, just use the same url above in your servlet (all you have to probably do is set the appropriate content type)
trojantiger
11-14-2008, 06:11 PM
Hi Khalid,
Thanks for the reply. I have a question, actually two.
How do I point to the URL? What functions can do this?
The perl script can be made to print content to the browser (it actually prints XML to a file, but it can also return it to the browser screen). How can I then read in the content to a variable in the Java servlet?
Do you have any sample code or sites that might help me understand how to do this?
Thank you!
trojantiger
11-14-2008, 10:03 PM
Oh! I figured it out! I was able to call my perl script through my little java servlet. It invokes the perl file, and reads in the XML output from that script and stores it into a string variable. COOL!
Now that I have the XML as a long string, I need to convert it to a JSON format.
Anyone know of a good way to do this?
trojanLion
11-15-2008, 12:24 AM
Can u please share with us the way to execute the perl script from java servlet
chazzy
11-15-2008, 12:09 PM
The most appropriate way of executing an external process (script, executable, etc) would be:
Runtime rt = Runtime.getRuntime();
rt.exec("your script");
There's a few different exec methods, so you should take a look at which might suit your needs.
anshuldhawan
11-19-2008, 08:23 PM
The exec call makes my server stop.
Can some one please tell me how can i invoke a perl script on some other server from a java servlet using urlconnection
chazzy
11-20-2008, 05:46 AM
for one, you need to have the perl script running on a server somewhere. most people use apache httpd. you can't embed it in your java app. from there, it's simply using java.net.URL to make the request.
AJ_Trojan
11-23-2008, 05:43 PM
Hi Khalid,
Thanks for the reply. I have a question, actually two.
How do I point to the URL? What functions can do this?
The perl script can be made to print content to the browser (it actually prints XML to a file, but it can also return it to the browser screen). How can I then read in the content to a variable in the Java servlet?
Do you have any sample code or sites that might help me understand how to do this?
Thank you!
Hi...can u please post the code for executing the perl script from a servlet !