尝试将applet与servlet通信时出现协议错误

时间:2015-12-10 13:20:32

标签: java servlets applet client-server

当我将我的名字从applet发送到servlet时,我面临“java.net.UnknownServiceException:protocol不支持输出”问题。 我试过Sending object to Servlet throws an error I cant resolve,但它没有解决我的问题。 我的applet代码是:

new Date(2015, 12, 10)

,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();
        }
    }
}

我几乎到处搜索但无法解决。

1 个答案:

答案 0 :(得分:0)

你在哪里运行applet?您是否真的将其嵌入到页面中并通过某种类型的Web服务器请求它?

代码

 URL urlServlet = new URL(getCodeBase(), "applettoservlet");
如果您正在从Web /应用程序服务器提供的页面运行applet,

将仅按预期工作。 尝试添加

System.out.println(urlServlet);

检查正在使用的网址。