0
当我从applet发送我的名字到servlet时,我面对“java.net.UnknownServiceException:协议不支持输出”的问题。 我试过Sending object to Servlet throws an error I cant resolve但它没有解决我的问题。 我的applet代码是:协议错误,试图沟通小程序到servlet
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class AppletToServlet extends HttpServlet {
// Getting a String object from the applet and send it back.
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
response.setContentType("application/x-java-serialized-object");
InputStream inputStream = request.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(inputStream);
String string = (String) inputFromApplet.readObject();
// getting string value and passing to applet
OutputStream outputStream = response.getOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(string);
objectOutputStream.flush();
objectOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
和servlet代码是:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class AppletToServletExample extends Applet {
private TextField inputField = new TextField(10);
private TextField resultField = new TextField(10);
public void init() {
// add input label, field and send button
add(new Label("Input Your Name", Label.RIGHT));
add(inputField);
Button sendButton = new Button("Send");
add(sendButton);
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SendData();
}
});
// add output label as a non editable field
add(new Label("Output:", Label.RIGHT));
add(resultField);
resultField.setEditable(false);
}
// Get a connection to the servlet.
private URLConnection getServletConnection() throws MalformedURLException,
IOException {
URL urlServlet = new URL(getCodeBase(), "applettoservlet");
URLConnection con = urlServlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type",
"application/x-java-serialized-object");
return con;
}
// Send the inputField data to the servlet and show the result in the
// outputField.
private void SendData() {
try {
String input = inputField.getText();
// send data to the servlet
URLConnection con = getServletConnection();
OutputStream outputStream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outputStream);
oos.writeObject(input);
oos.flush();
oos.close();
// receive result from servlet
InputStream inputStream = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(
inputStream);
String result = (String) inputFromServlet.readObject();
inputFromServlet.close();
inputStream.close();
// show result
resultField.setText(result);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
我搜索几乎无处不在,但没能解决它。
不,我想在neatbeans applet查看器上运行它。 不在浏览器中。 那该怎么办? – zohaibkhan
它没有帮助解决我的问题 – zohaibkhan
我想有更好的方法来做到这一点,但我会硬编码的URL到服务器进行测试。 替换 'URL urlServlet =新的URL(getCodeBase(), “applettoservlet”);' 与 'URL urlServlet =新的URL( “HTTP:// myappserver/myServletPath/applettoserver”);' – fiffy