Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-14-2002, 10:40 AM   PM User | #1
Sscotties
New Coder

 
Join Date: Aug 2002
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Sscotties is an unknown quantity at this point
Is this a Perl problem or an Apache problem?

Calling Applet to server. Make my Applet work.
Somebody help me please! I'm a student and I know that Applets aren't
really meant to behave this way - and even if they do, they may not be
allowed to due to local Firewall restrictions. So, my appeal for help
is as much academic as it is practical. So, please don't tell me about
servlets or the magic of JSP. I want my Applet to talk! The Applet
program is listed here (below) and this accesses a Perl cgi-bin script
on my localhost (127.0.0.1) Apache server. I am using Windows (98 se) and
have created a public_html folder and even tried with a opinion.txt file;-

#! C:\perl\bin\perl.exe
open(OUT, "> /public_html/opinion.txt");
print "content-type: text/plain\n\n";

while (<>) {
print OUT $_;
print $_;
}
close (OUT);
exit 0;
# end of script


I'm a total beginner with Perl, so I don't have the first clue how to
correct the Perl Script, if it is wrong. When I check the server log,
it says it has accessed the script with the following message;-

"POST /cgi-bin/wdwrite.pl HTTP/1.1" 200 64



Here, also is the Applet. It's quite a simple progam;-

/** postMethod.java - see also wdwrite.pl
* Textfied and button. The button calls to a function writePage()
* this then gets the host() and opens a URL connection to send the
* data out, back to the place the Applet came with a dataOutputStream
* object and writeBytes() and linked back to a Perl cgi-bin program.
*/

import java.applet.Applet;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*; //Tidy these up later with .ActionEvent etc;

public class postMethod extends Applet implements ActionListener {
TextArea info;
Button Bsend;
String str;
String host, conext, exc1, exc2;

//*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X
// init the textfield and button. Only the button needs ActionListener

public void init() {
info = new TextArea(5,68);
add(info);
Bsend = new Button("SEND");
Bsend.addActionListener(this);
add(Bsend);
}

//X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X
// show the results or exceptions in a series of strings

public void paint (Graphics g) {
g.drawString("Got this: "+str, 30, 150); // message 2B sent
g.drawString(host, 30, 170); // getHost().getCodeBase()
g.drawString(conext, 30, 190); // getConnection.toSting()
g.drawString(exc1, 30, 210); // didn't like the call to host
g.drawString(exc2, 30, 230); // didn't like the writeBytes()
}

//*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*
// button links to the Post Method and calls the function writePage()

public void actionPerformed(ActionEvent e) {
if (e.getSource() == Bsend) {
str = info.getText();
try {
writePage(); // Has to be caught
}
catch (Exception e1) {
exc1 = "writePage: " + e1.toString();
}
}
repaint(); // exceptions /results etc
}

//*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*X*
// writePage function returns to repaint. Links the page back to
// whence it came and directs it to the Perl script in cgi-bin
// Note: try-catch are not necessary here. ie: it should still work
// without this. The purpose is to catch an error message should
// an error occur. Sent as a String in repaint() g.drawString

public void writePage() throws Exception {
host = getCodeBase().getHost();
try {
URL url = new URL("http://"+host+"/cgi-bin/wdwrite.pl");
URLConnection con = url.openConnection();
conext = "connect OK: " + con.toString();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-type", "text/plain");
con.setRequestProperty("Content-length", str.length()+" ");

DataOutputStream out = new DataOutputStream(con.getOutputStream());
String content = URLEncoder.encode(str);
out.writeBytes(content);
out.flush();
out.close();
exc2 = "Seems OK: reads to final line.";
}
catch (Exception e2){
exc2 = "dataOutputStream: " + e2.toString();
} // end try-catch
} // end writePage()
}
Sscotties is offline   Reply With Quote
Old 08-15-2002, 02:18 AM   PM User | #2
pager
New Coder

 
Join Date: Aug 2002
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
pager is an unknown quantity at this point
http://www.frii.com/~michael/how_to_communicate.htm

There's some more references to this if you type in a search engine:

applet stdin cgi URLConnection

--------------------------------------------

What you have to do is pass the java input to STDIN (which is what your <> is trying to read).... The above will show you how.

Believe it or not... Applets behave this way all the time. You just need to have your interface set up on cgi... with the comm parameters on the applet set properly.

Good luck.

Last edited by pager; 08-15-2002 at 02:37 AM..
pager is offline   Reply With Quote
Old 08-21-2002, 12:52 AM   PM User | #3
Sscotties
New Coder

 
Join Date: Aug 2002
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Sscotties is an unknown quantity at this point
Thanx 2 Pager 4 taking the time to reply

And as for;- "Believe it or not... Applets behave this way all the time." I have no problem in believing this at all and have come to the conclusion that applet technology is pretty naff.

Since the posting I have managed to get some kind of server side response with this ;-
"yes+it+will+do+it+%21%0A"
- which isn't very useful, but at least I know the java code is correct.

If you have any further ideas I will check again soon and in the mean time I shall check the link that you supplied and try and get my head around PERL. Actually Java was my first language, so I find PERL a bit strange - though I have used worse - Ever tried Lingo for Director?

DON'T!
Sscotties is offline   Reply With Quote
Old 08-21-2002, 02:48 AM   PM User | #4
pager
New Coder

 
Join Date: Aug 2002
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
pager is an unknown quantity at this point
You'll have to de-elevate your thought process to do it because the syntax is more unix like than anything else.... Actually, if you can learn java or c, you can definitely work with perl and may find it quite a refreshing change for getting into a different scripting language.

The thing to remember with cgi/perl though, is that this is the blood and guts of server/internet i/o. Which is why it ain't ded yet.

lol.
pager is offline   Reply With Quote
Old 08-21-2002, 10:47 AM   PM User | #5
Sscotties
New Coder

 
Join Date: Aug 2002
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Sscotties is an unknown quantity at this point
Blood and guts

I've noticed that Perl is still very much alive and kicking. Some say its coz this is what the Internet magicians learned first and others say its coz its robust. The first law of engineering: 'if it works, DON'T TOUCH IT!'.

Is there a question in the vacinity of this reply? - yes.

There are so many languages out there that it becomes hard to know which one to learn and which to avoid. Some say that php is becoming the web friendly lingua franca - and I have dabbled ...it certainly has advantages over perl, in that it doesnt look like a Bulgarian dictionary.

What do you think - or anyone else out there !!! I agree that Perl won't be disappearing soon and it is very well supported. Should I swim with Perl or surf with php?
Sscotties is offline   Reply With Quote
Old 08-22-2002, 12:27 PM   PM User | #6
Sscotties
New Coder

 
Join Date: Aug 2002
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Sscotties is an unknown quantity at this point
ACTUALLY I THINK IT WAS A WINDAS PROBLEM

When I used the absolute path - then Windows seemed to get it and direct it to the right place. ie:

#! C:\perl\bin\perl.exe

$address = "C:/Apache Group/Apache/htdocs/page.html";
open(OUT, ">$address");
print "content-type: text/plain\n\n";

while (<>) {
print OUT $_;
print $_;
}
close (OUT);
exit 0;

Thanx for your support /concern and encouragement
- I thought I'd publish the solution just in case others may come here with similar problems
Sscotties is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:20 PM.


Advertisement
Log in to turn off these ads.