关于APPLET调用SERVLET的问题,请高手帮忙!在线等待
ccsxg 2003-03-12 08:45:03 我用APPLET调用SERVLET
为什么JRE抱HTTP405错误?
下面是我的APPLET,难道APPLET调用SERVLET也要有安全限制吗?我的这段代码怎么才能调用servlet?需要安全配置文件吗?
package applettest;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class SaleBillForm
extends Applet {
private boolean isStandalone = false;
XYLayout xYLayout1 = new XYLayout();
JTextField jTextField1 = new JTextField();
JButton jButton1 = new JButton();
JTextArea jTextArea1 = new JTextArea();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public SaleBillForm() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setLayout(xYLayout1);
jButton1.setText("查询");
jButton1.addActionListener(new SaleBillForm_jButton1_actionAdapter(this));
jTextField1.setText("");
jTextArea1.setText("");
this.add(jTextField1, new XYConstraints(16, 22, 279, 27));
this.add(jButton1, new XYConstraints(314, 24, 57, -1));
this.add(jTextArea1, new XYConstraints(19, 90, 276, 171));
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
//Main method
public static void main(String[] args) {
SaleBillForm applet = new SaleBillForm();
applet.isStandalone = true;
Frame frame;
frame = new Frame();
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400, 320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation( (d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
void jButton1_actionPerformed(ActionEvent e) {
String qryString = jTextField1.getText();
try {
URL url = new URL("http://localhost:8080/applet/servlet/applettest.DBServlet");
String qry = URLEncoder.encode("qry") + "=" + URLEncoder.encode(qryString);
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
DataOutputStream dos = new DataOutputStream(uc.getOutputStream());
dos.writeBytes(qry);
dos.flush();
dos.close();
InputStreamReader in = new InputStreamReader(uc.getInputStream());
int chr = in.read();
jTextArea1.setText("begin");
while (chr != -1) {
jTextArea1.append(String.valueOf( (char) chr));
chr = in.read();
}
in.close();
}
catch (MalformedURLException me) {
System.out.println(me.getMessage());
}
catch (IOException ie) {
System.out.println(ie.getMessage());
}
}
}
class SaleBillForm_jButton1_actionAdapter
implements java.awt.event.ActionListener {
SaleBillForm adaptee;
SaleBillForm_jButton1_actionAdapter(SaleBillForm adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}