将值从applet传递给jsp

时间:2011-04-16 11:31:26

标签: java jsp applet

import java.awt.*; 
import java.applet.*;

public class GuiExample extends Applet { 
    Button okButton; 
    TextField nameField;

    public void init() { 
        setLayout(null); 
        okButton = new Button("A button"); 
        nameField = new TextField("A TextField",100); 
        okButton.setBounds(20,20,100,30); 
        nameField.setBounds(20,70,100,40); 

        add(okButton); 
        add(nameField); 
    }

}

当单击按钮时,如何将文本框中的值传递给'validate.jsp'? 我还希望浏览器转到那个jsp页面。并从那里继续执行? 我该如何修改代码?

1 个答案:

答案 0 :(得分:0)

这段代码可能会有所帮助。

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

public class GuiExample extends Applet { 
    Button okButton; 
    TextField nameField;
    GuiExample currentApplet; // needed for navigation

    public void init() {
        currentApplet = this;
        setLayout(null); 
        okButton = new Button("A button"); 
        nameField = new TextField("A TextField",100); 
        okButton.setBounds(20,20,100,30); 
        nameField.setBounds(20,70,100,40); 

        add(okButton); 
        add(nameField);

        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text = nameField.getText();
                if (text != null) {
                    try {
                        // modify this to point to your website
                        String address = "http://www.example.com/validate.jsp?p=" + URLEncoder.encode(text, "UTF-8");
                        currentApplet.getAppletContext().showDocument(new URL(address));
                    } catch (Exception ex) {
                    }
                }
            }
        });

    }

}