简体   繁体   English

从Java Applet调用PHP脚本?

[英]Invoking a php script from java applet?

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( ));
}

 }

The code above is a java applet. 上面的代码是一个Java applet。 That is a GUI for name last name email enterence.It has a button. 这是用于姓氏电子邮件输入的GUI。它具有一个按钮。 I want to call a php script -save.php- when the button is clicked. 单击该按钮时,我想调用一个php脚本-save.php-。 I tried it in my actionPerformed method but it doesnt work. 我在我的actionPerformed方法中尝试过,但是它不起作用。

How can i do it. 我该怎么做。

Here is my php script below 这是我的以下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' ) ;
?>

I m not sure that the script is also correct? 我不确定脚本是否正确?

The applet works properly without php script invocation. 该小程序无需php脚本调用即可正常工作。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM