kontaktformular i javaapplet
Hejsajeg har forgæves forsøgt at finde en klassisk applet kontaktformular, hvor brugeren kan kontakte mig ved at indtaste navn og adresse osv. og ved tryk på \'send\' knappen modtager jeg indholdet. Den eneste kode jeg har kunne finde er opstillet som denne, men den kan jeg sgu\' ikke få til at virke, idet kompileren i metoden goconnection fanger exceptionen istedet for at connecte til mailserveren. I html-koden har jeg CODEBASE=\"http://formmail.andersenit.dk/formmail.pl\" som smtp. Er der én der kan se, om den er gal i følgende kode?:
import java.io.*;
import java.net.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Kontakt extends Applet implements ActionListener
//GUI-class kontaktformular
{
BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
//Tekstfelter
TextField Navn1;
TextField Adresse1;
TextField Postnr1;
TextField By1;
TextField Tlf1;
TextField Email1;
TextField Kommentar1;
String bocenmail = \"bocen@bocen.dk\";
//knapper
private Button Send;
//Labels
Label Navn;
Label Adresse;
Label Postnrby;
Label Tlf;
Label Email;
Label Kommentar;
Label Besked;
public Component[] compo;
//constructor
public Kontakt()
{
//resize(150,180);
setLayout(null);
setBackground(Color.black);
Navn = new Label(\"Navn:\");
Navn.setBounds(50,40,70,22);
add(Navn);
Navn.setForeground(Color.white);
Adresse = new Label(\"Adresse:\");
Adresse.setBounds(50,70,70,22);
add(Adresse);
Adresse.setForeground(Color.white);
Postnrby = new Label(\"Postnr by:\");
Postnrby.setBounds(50,100,70,22);
add(Postnrby);
Postnrby.setForeground(Color.white);
Tlf = new Label(\"Tlf:\");
Tlf.setBounds(50,130,70,22);
add(Tlf);
Tlf.setForeground(Color.white);
Email = new Label(\"E-mail:\");
Email.setBounds(50,160,70,22);
add(Email);
Email.setForeground(Color.white);
Kommentar = new Label(\"Kommentar:\");
Kommentar.setBounds(50,190,70,22);
add(Kommentar);
Kommentar.setForeground(Color.white);
Navn1 = new TextField();
Navn1.setBounds(122,40,140,22);
add(Navn1);
Navn1.setEditable(true);
Navn1.setBackground(Color.white); // En \"tastelytter\" tilknyttes tekstfeltet.
Adresse1 = new TextField();
Adresse1.setBounds(122,70,140,22);
add(Adresse1);
Adresse1.setEditable(true);
Adresse1.setBackground(Color.white);
Postnr1 = new TextField();
Postnr1.setBounds(122,100,33,22);
add(Postnr1);
Postnr1.setEditable(true);
Postnr1.setBackground(Color.white);
By1 = new TextField();
By1.setBounds(160,100,103,22);
add(By1);
By1.setEditable(true);
By1.setBackground(Color.white);
Tlf1 = new TextField();
Tlf1.setBounds(122,130,75,22);
add(Tlf1);
Tlf1.setEditable(true);
Tlf1.setBackground(Color.white);
Email1 = new TextField();
Email1.setBounds(122,160,140,22);
add(Email1);
Email1.setEditable(true);
Email1.setBackground(Color.white);
Kommentar1 = new TextField();
Kommentar1.setBounds(122,190,140,82);
add(Kommentar1);
Kommentar1.setEditable(true);
Kommentar1.setBackground(Color.white);
Send = new Button(\"Send\");
Send.setBounds(165,280,50,22);
add(Send);
Send.setBackground(Color.lightGray);
Send.setForeground(Color.black);
Send.addActionListener(this);
Besked = new Label(\" \");
Besked.setBounds(100,15,150,22);
add(Besked);
Besked.setForeground(Color.white);
}
public void init()
{
compo=new Component[7];
compo[0]=Navn1;
compo[1]=Adresse1;
compo[2]=Postnr1;
compo[3]=By1;
compo[4]=Tlf1;
compo[5]=Email1;
compo[6]=Kommentar1;
compo[0].requestFocus();
if(compo[0] instanceof TextComponent) ((TextComponent)compo[0]).selectAll();
}//end init
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==Send)
{
String sNavn1=Navn1.getText();
String sAdresse1=Adresse1.getText();
String sPostnr1=Postnr1.getText();
String sBy1=By1.getText();
String sTlf1=Tlf1.getText();
String sEmail1=Email1.getText();
String sKommentar1=Kommentar1.getText();
if(sNavn1.length()==0 || sAdresse1.length()==0 || sPostnr1.length()==0 || sBy1.length()==0 || sTlf1.length()==0 || sEmail1.length()==0 || sKommentar1.length()==0)
{
Besked.setText(\"Please fill all the fields!\");
}
Send.setEnabled(false);//to prevent connecting twice
//if Send clicked, it establish the connection
//and send the data to the server
goConnection(sNavn1, sAdresse1, sPostnr1, sBy1, sTlf1, sEmail1, sKommentar1);
}
}//end handleEvent
//if the user try to load another page, it continue the work
public void stop()
{
return;
}
// the reply from the server
boolean readReply(DataInputStream in)
{
String line=\"\";
try
{
line=input.readLine();
}
catch(IOException e)
{
Besked.setText(\"Reading reply problem\");
Send.setEnabled(true);
return true;
}
if(line.startsWith(\"4\") || line.startsWith(\"5\"))
{
Besked.setText(\"Err: \"+line);
Send.setEnabled(true);
return true;
}
else return false;
}//end readReply
public void goConnection(String navn, String adresse, String postnr, String by, String tlf, String email, String kommentar)
{
try
{
Besked.setText(\"Establishing connection..\");
Socket sock=new Socket(getCodeBase().getHost(), 25);
DataOutputStream out=new DataOutputStream(sock.getOutputStream());
DataInputStream in=new DataInputStream(sock.getInputStream());
Besked.setText(\"Connection OK, sending data\");
if(readReply(in))
{
sock.close();
return;
}
out.writeBytes(\"ATT: Bocen.dk\\r\\n\");
if(readReply(in))
{
sock.close();
return;
}
out.writeBytes(\"MAIL FROM:<\"+navn+\">\\r\\n\");
if(readReply(in))
{
sock.close();
return;
}
out.writeBytes(\"<\"+adresse+\">\\r\\n\");
if(readReply(in))
{
sock.close();
return;
}
out.writeBytes(\"<\"+postnr+\">\\r\\n\");
if(readReply(in))
{
sock.close();
return;
}
out.writeBytes(\"<\"+by+\">\\r\\n\");
if(readReply(in))
{
sock.close();
return;
}
out.writeBytes(\"<\"+tlf+\">\\r\\n\");
if(readReply(in))
{
sock.close();
return;
}
out.writeBytes(\"RCPT TO:<\"+email+\">\\r\\n\");
if(readReply(in))
{
sock.close();
return;
}
out.writeBytes(\"DATA\\r\\n\");
if(readReply(in))
{
sock.close();
return;
}
//if(adresse!=null) out.writeBytes(\"Subject: \"+adresse+\"\\n\\n\");
out.writeBytes(kommentar+\"\\r\\n.\\r\\nQUIT\\r\\n\");
if(readReply(in))
{
sock.close();
return;
}
sock.close();
Besked.setText(\"E-mail sent, you can write another message\");
Send.setEnabled(true);
}
catch(IOException e)
{
Besked.setText(\"Connection problem or connection closed\");
Send.setEnabled(true);
}
};//end goConnection
}//end class Kontakt
Den er godt nok lidt lang, men der er vel nogle derude med falke-overblik.
