Java 将对象发送到Servlet会引发一个我无法解决的错误

Java 将对象发送到Servlet会引发一个我无法解决的错误,java,servlets,applet,Java,Servlets,Applet,我正在将字符串从小程序发送到Servlet。当我从URLConnection获取输出流时,会抛出异常java.net.UnknownServiceException:protocol不支持输出 一些背景资料;我正在使用Eclipse,我通过在Eclipse中运行它来测试小程序&在我自己制作的html页面中&它们抛出相同的错误。我已经下载了正确的JavaWebSDK。也许我需要建立我的hxxp://8008... 服务器 为什么会发生这种情况&我如何修复它?我需要为小程序签名才能使其工作吗 这是我

我正在将字符串从小程序发送到Servlet。当我从URLConnection获取输出流时,会抛出异常java.net.UnknownServiceException:protocol不支持输出

一些背景资料;我正在使用Eclipse,我通过在Eclipse中运行它来测试小程序&在我自己制作的html页面中&它们抛出相同的错误。我已经下载了正确的JavaWebSDK。也许我需要建立我的hxxp://8008... 服务器

为什么会发生这种情况&我如何修复它?我需要为小程序签名才能使其工作吗

这是我的代码&我已经对引发异常的位置进行了注释:

public String messageServlet()
{
    try
    {
        URLConnection conn = connectToServlet();
        conn.setDoOutput(true);
        OutputStream out = conn.getOutputStream();  // Exception thrown here: UnknownServiceException: protocol doesn't support output
        ObjectOutputStream objOut = new ObjectOutputStream( out );
        objOut.writeObject( message );
        objOut.flush();
        objOut.close();

        System.out.println( "1" ); 

        // receive result from servlet
        InputStream instr = conn.getInputStream();
        ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
        String result = (String) inputFromServlet.readObject();
        inputFromServlet.close();
        instr.close();
        System.out.println( "1" );
        return result;
    }
    catch ( IOException e )
    {
        System.out.println( "In messageServlet(): " + e );
        msgBox.setText( msgBox.getText() + "\nIn messageServlet(): " + e );
    }
    catch ( Exception e )
    {
        System.out.println( "In messageServlet(): " + e );
        msgBox.setText( msgBox.getText() + "\nIn messageServlet(): " + e );
    }

    return null;
}

public URLConnection connectToServlet()
{
    try
    {
        URL servletUrl = new URL( getCodeBase(), "echo" );
        URLConnection conn = servletUrl.openConnection();

        conn.setDoInput( true );
        conn.setDoOutput( true );
        conn.setUseCaches( false );
        conn.setRequestProperty( "Content-Type", "application/x-java-serialized-object" );

        return conn;
    }
    catch ( IOException e )
    {
        System.out.println( "In connectToServlet(): " + e );
    }

    return null;
}

您有两个潜在问题:

  • Web服务器未启动。确保它已启动并且在webbrowser中正常工作

  • 您使用了错误的URL。这个计划毫无意义。是的


  • 除此之外,通常的做法是不要在applet中硬编码基本域,这会使其不可移植(每次移动域时都需要修复/更改它)。只需从继承的方法获取它。将其用作servlet URL的基础

    URL servlet = new URL(getCodeBase(), "servleturl");
    // ...
    
    因此,这里的
    getCodeBase()
    返回值如下



    除此之外,我更喜欢发送明文、JSON或XML格式而不是HTTP格式的Java特定二进制数据。它具有更好的可重用性,并且更容易进行前/后处理。您希望在字符串中来回发送一些字符。只需将其作为HTTP请求参数发送,并让servlet通过
    request.getParameter()
    等方式获取它。为什么要为此使用Java序列化?

    您有两个潜在的问题:

  • Web服务器未启动。确保它已启动并且在webbrowser中正常工作

  • 您使用了错误的URL。这个计划毫无意义。是的


  • 除此之外,通常的做法是不要在applet中硬编码基本域,这会使其不可移植(每次移动域时都需要修复/更改它)。只需从继承的方法获取它。将其用作servlet URL的基础

    URL servlet = new URL(getCodeBase(), "servleturl");
    // ...
    
    因此,这里的
    getCodeBase()
    返回值如下



    除此之外,我更喜欢发送明文、JSON或XML格式而不是HTTP格式的Java特定二进制数据。它具有更好的可重用性,并且更容易进行前/后处理。您希望在字符串中来回发送一些字符。只需将其作为HTTP请求参数发送,并让servlet通过
    request.getParameter()
    等方式获取它。为什么要为此使用Java序列化?

    来自文档:。。。应用程序正在尝试写入只读URL连接。我想你必须先设置好你的服务器。此代码正在使用有效的代码。如何将连接设置为非只读?这是我可以用Java做的,还是与我的实际互联网连接有关?你能打印你的URL吗?如果它是某种类型的
    文件:
    URL,它当然不能工作。如果它是
    http:
    URL,它应该可以工作(或者你会得到更好的错误消息)。感谢您的评论:yeah my URL=file:/C:/Users/Print/Desktop/TestApplet/bin/。我觉得有点傻,但我怎样才能把它转到http:。。。。?我知道我可以写“http://...“但是我想要点什么dynamic@Paŭlo Ebermann将连接更改为URL servletUrl=新URL(“hxxp://localhost:8008/" ); 导致文档中出现以下错误“java.net.ConnectException:连接被拒绝:connect”:。。。应用程序正在尝试写入只读URL连接。我想你必须先设置好你的服务器。此代码正在使用有效的代码。如何将连接设置为非只读?这是我可以用Java做的,还是与我的实际互联网连接有关?你能打印你的URL吗?如果它是某种类型的
    文件:
    URL,它当然不能工作。如果它是
    http:
    URL,它应该可以工作(或者你会得到更好的错误消息)。感谢您的评论:yeah my URL=file:/C:/Users/Print/Desktop/TestApplet/bin/。我觉得有点傻,但我怎样才能把它转到http:。。。。?我知道我可以写“http://...“但是我想要点什么dynamic@Paŭlo Ebermann将连接更改为URL servletUrl=新URL(“hxxp://localhost:8008/" ); 导致此错误“java.net.ConnectException:连接被拒绝:连接”