• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to call a Perl program in Servlet?

 
Henry Zhang
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gurus-
I am struggling for this over ten days. Please help me.(^_^)
Everything else is fine, except for the java runtime part (towards the end of the file). The purpose of GQL.java is to take user input from the web interface and query the Oracle DB in my account, then save the SQL query result into a text file called "foo.txt" into my public_html directory; then I need to call a Perl script "newfup.cgi" which resides under the same path as GQL.java, to take the file "foo.txt" as input and generate a GIF file called "plotfile.gif" and put this GIF file to my public_html directory so that the user can see this image file as the final query result.
There must be something wrong with the runtime call. I could execuate the Perl script on the command line by "/usr/bin/perl newfup.cgi". I could get the image file plotfile.gif generated and saved under my public_html directory this way. But it just wouldn't work out from execuating the servlet code.
------
import java.net.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import oracle.jdbc.driver.*;
public class GQL extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
...
try
{
String cmd = "/usr/bin/perl newfup.cgi";
Runtime r = Runtime.getRuntime();
Process p = r.exec(cmd);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String tmp = new String();
while (tmp != null)
{
tmp = in.readLine();
}
out.println("<P>");
out.println("<CENTER>");
out.println("<img src=\"http://...myAccount.../plotfile.gif\">");
out.println("<P>");
out.println("<img src=\"http://...myAccount.../bully.gif\">");
out.println("</CENTER>");
}

catch (Exception e)
{
e.printStackTrace();
}
...
}
}
[This message has been edited by Henry Zhang (edited February 24, 2001).]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know something like this can work because I have an example in my servlets book. Your problem may lie in this:
"then I need to call a Perl script
"newfup.cgi" which resides under the same path as GQL.java,"
When a servlet engine is running, the "current directory" can be anything the servlet engine wants. I think you should try an absolute path to the perl script.
Bill

------------------
author of:
 
Henry Zhang
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill-
Thank you very much for your help. I'll buy the book you wrote. BTW, your Java 2 Exam Cram did help me a lot to pass the SCPJ2.
Thank you for your consistent contribution for this forum.
 
liz munroe
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Change this line:
String cmd = "/usr/bin/perl newfup.cgi";
to
String[] cmd ={"/usr/bin/perl","/fullpath/to/file/newfup.cgi"};
(and /fullpath/to/file is the absolute path from the root directory).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic