The Artima Developer Community
Sponsored Link

Java Answers Forum
Applet writing to Server: please check my code

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Simon Scott

Posts: 1
Nickname: skoddy
Registered: Aug, 2002

Applet writing to Server: please check my code Posted: Aug 13, 2002 2:36 PM
Reply to this message Reply
Advertisement
Calling Applet to server.
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;-

#! 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*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*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*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*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()
}

Topic: Tomcat 4.0.4 Classpath Previous Topic   Next Topic Topic: Question of the Day

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use