我想從一個applet調用一個servlet,然後顯示servlet在applet gui中的響應,問題是當我按下按鈕調用servlet時,GUI塊直到響應到達,我嘗試把請求代碼放在一個線程中,但是它凍結了gui,這是代碼:Applet和Servlet通信塊GUI
public class SearchApplet extends JApplet implements ActionListener{
JTextField JTF_address;
public void init() {
JLabel JL_address = new JLabel("Address: ");
JL_address.setFont(new Font("OpenSans", Font.PLAIN, 16));
JTF_address = new JTextField(20);
....
}
public void actionPerformed(ActionEvent e) {
SearchThread search = new SearchThread();
search.run();
}
public class SearchThread implements Runnable {
public SearchThread() {
}
public void run() {
try {
String input = "prova";
URLConnection con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(input);
oos.flush();
oos.close();
// receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
String result = (String) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();
// show result
JTF_address.setText(result);
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex.toString());
}
}
}
}
我該怎麼辦?
移動你的通訊出來的GUI線。 – keshlam
我該怎麼做? – Piero
我認爲,「我如何編寫多線程應用程序」太籠統,也是Stack Overflow的一個問題。在Java中有很多關於多線程編程的教程... – keshlam