gpt4 book ai didi

java - 将值从 applet 传递到 jsp

转载 作者:行者123 更新时间:2023-12-01 05:47:01 24 4
gpt4 key购买 nike

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页面。并从那里继续执行?如何修改代码?

最佳答案

这段代码可能会有帮助。

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

}

}

关于java - 将值从 applet 传递到 jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5686165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com