Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

My Java applet previously used param (HTML), example ;-

<applet code="family.class" width=610 height=400> <param name="nodes" value="10"> <!-- 0=unique id (1 based) 1=text 2=parent id 3=first son id (0=none) 4=next brother id (0=none) 5=depth level (0 based) --> <param name="1" value="1|Monica|0|2|0|0"> <param name="2" value="2|Ion|1|0|3|1"> <param name="3" value="3|Mihai|1|6|4|1"> <param name="4" value="4|Filip|1|8|5|1"> <param name="5" value="5|Silvia|1|0|0|1"> <param name="6" value="6|Diana|3|0|7|2"> <param name="7" value="7|Simion|3|0|9|2"> <param name="8" value="8|Alexandru|4|0|0|2"> <param name="9" value="9|Mihaela|3|0|10|2"> <param name="10" value="10|Andrei|3|0|0|2"> </applet>

But recently I got to know that Perl could do a better job than HTML. Perl could even extract data from database and send it to applet.

I am trying to do that, so what change do I have to do in the perl script and java applet

TQ

Replies are listed 'Best First'.
Re: Applet to Perl(cgi)
by BUU (Prior) on Jun 14, 2005 at 17:05 UTC
    It looks like you just need to write a perl script that prints out html in the form you want, assuming this is just a static, one way communication. Otherwise you're going to need your applet to load some HTTP libraries and send data to the server and parse the results.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Applet to Perl(cgi)
by ikegami (Patriarch) on Jun 14, 2005 at 17:45 UTC

    Java applets execute on the browsing machine, whereas Perl CGI programs execute on the web server machine. A simple way to send data to a Java applet from a CGI program is to include the data as paramaters to the applet in the HTML. You can use Perl (under CGI) to create this HTML dynamically, as shown in the following code.

    use DBI; my $dbh = DBI->connect(...); my $data = $dbh->selectall_arrayref(<<'__EOS__'); SELECT id, name, parent, first_son, next_brother, depth FROM family_tree WHERE ... __EOS__ my $count = @$data; print(<<"__EOS__"); Content-Type: text/html <applet code="family.class" width=610 height=400> <param name="nodes" value="$count"> <!-- 0=unique id (1 based) 1=text 2=parent id 3=first son id (0=none) 4=next brother id (0=none) 5=depth level (0 based) --> __EOS__ my $param = 1; foreach my $fields (@$data) { my $name = $param++; my $value = join('|', @$fields); print("<param name=\"$param\" value=\"$value\">\n"); } print("</applet>\n");
Re: Applet to Perl(cgi)
by Errto (Vicar) on Jun 15, 2005 at 01:46 UTC
    If you need to update the data dynamically, you can have the applet make an HTTP call to the Perl CGI running on the server. In that case, Perl should output the data as plain text instead of HTML. So on the server side your code would look like (adopting ikegami's example):
    print "Content-type: text/plain\n\n"; my $param = 1; foreach my $fields (@$data) { my $name = $param++; my $value = join('|', @$fields); print "$value\n"; }
    and on the client, you would have a method to retrieve the data (assuming your users have the Java Plug-in with JRE1.4 or above):
    import java.net.URL; import java.net.HttpURLConnection; import java.io.BufferedReader; import java.io.InputStreamReader; ... URL url = new URL(getCodeBase(), "/cgi-bin/getmydata.pl"); // or whate +ver HttpURLConnection con = (HttpURLConnection) con.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getIn +putStream())); String s; int count = 0; while ((s = in.readLine()) != null) { String[] fields = s.split("\\|"); count++; // do something with count and fields } in.close(); con.disconect();
    This takes advantage of the fact that when you use a URLConnection in an applet, it automatically picks up the appropriate proxy settings, user-agent, cookies etc from the browser. Also remember that the CGI will have to live on the same server as the applet unless the applet is signed (for security reasons).
      Does this mean, anytime the data is change in database, the applet will automatically change without using refresh/reload?
        It will automatically change if you set your applet to make this http call periodically and then do something with the result. The code I showed only retrieves the data from the server once, so you'll have to write the code to call it once every however often you want.
Re: Applet to Perl(cgi)
by Anonymous Monk on Jun 14, 2005 at 17:39 UTC
    This is part og my Java code and Perl Script(testing)
    nodes_s = getParameter("nodes"); //citeste numarul de noduri if (nodes_s==null) //daca nu exista parametrul { error_msg="Need parameter 'NODES' [1]"; } else if (nodes_s.equals("")) //daca nu contine nimic { error_msg="Need parameter 'NODES' [2]"; } try { nodes = Integer.parseInt(nodes_s); } catch (NumberFormatException e) //daca nu e un numar { error_msg="Parameter 'NODES' must be numeric [3]"; } ndata = new String[nodes+1][9]; //defineste matricea de date for (i=1;i<nodes+1;i++) //o parcurgem in sens crescator { s=getParameter(""+i); //citim parametrul i if (s==null) //daca nu exista { error_msg="Need parameter '"+i+"' [4]"; break; } else if (s.equals(
    -----------------------------------
    $nodes1 = "1|Monica|0|2|0|0"; $nodes2 = "2|Ion|1|0|3|1"; $nodes3 = "3|Mihai|1|6|4|1"; print "$nodes1~|~$nodes2~|~$nodes3~|~