从java applet调用php脚本?

时间:2012-10-23 02:52:07

标签: java php applet invoke

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class RegisterUser extends Applet{



TextField panel1 = new TextField(10);
TextField panel2 = new TextField(10);
TextField panel3 = new TextField(10);
Button    save   = new Button("Save");



public void init(){

    add(panel1);
    add(new Label("Name:"));
    addNewLine();
    add(panel2);
    add(new Label("Last:"));
    addNewLine();
    add(save);
    addNewLine();



save.addActionListener(new SaveListener());
}

class SaveListener implements ActionListener{

    public void actionPerformed(ActionEvent event){

    URL myURL = new URL("http://myplace/2272/save.php");
        URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();


}
}


private void addHorizontalLine(Color c)
{
  Canvas line = new Canvas( );
  line.setSize(10000,1);
  line.setBackground(c);
  add(line);
}


private void addNewLine( )
{
  addHorizontalLine(getBackground( ));
}

 }

上面的代码是一个java applet。这是名字姓氏电子邮件enterence的GUI。它有一个按钮。我想在单击按钮时调用php脚本-save.php-。我在我的actionPerformed方法中尝试了它,但它不起作用。

我该怎么做。

这是我下面的php脚本

<?php
session_start();

$fp = fopen('users.txt', 'a+');
fwrite($fp, "\nname: " . $_GET["name"] . "\n");
fwrite($fp, "last name: " . $_GET["last_name"] . "\n");
fwrite($fp, "email: " . $_GET["email"] . "\n");

fclose($fp);
$_SESSION['registered']=1;
header( 'Location: http://myplace2272/save.php' ) ;
?>

我不确定脚本是否正确?

applet在没有php脚本调用的情况下正常工作。

1 个答案:

答案 0 :(得分:3)

当您调用save.php时,您需要使用它传递参数:

URL myURL = new URL("http://myplace/2272/save.php?name="+panel1.getText()+"&last_name="+panel2.getText()+"&email="+panel3.getText());