发送对象的Servlet抛出一个错误,我不能化解 [英] Sending object to Servlet throws an error I cant resolve

查看:189
本文介绍了发送对象的Servlet抛出一个错误,我不能化解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我送从一个Applet到Servlet的字符串。当我去得到我得到的异常抛出的URLConnection的输出流的 java.net.UnknownServiceException:协议不支持输出

一些背景信息;我使用Eclipse,我在日食和放大器运行它测试的小程序;我在自己的网页上我犯了和放大器;他们扔了同样的错误。我已经下载了正确的Java Web SDK。也许我需要设置我的hxxp:// 8008 ...服务器

这是怎么回事&放大器;我该如何解决?我需要我签字的小程序,使其工作?

下面是我的code&放;我评论过的地方抛出异常:

 公共字符串messageServlet()
{
    尝试
    {
        康涅狄格州的URLConnection = connectToServlet();
        conn.setDoOutput(真);
        OutputStream的OUT = conn.getOutputStream(); //抛出异常的位置:UnknownServiceException:协议不支持输出
        ObjectOutputStream的objOut =新的ObjectOutputStream(出);
        objOut.writeObject(消息);
        objOut.flush();
        objOut.close();        的System.out.println(1);        //接收来自servlet的结果
        InputStream的INSTR = conn.getInputStream();
        ObjectInputStream的inputFromServlet =新的ObjectInputStream(INSTR);
        字符串结果=(字符串)inputFromServlet.readObject();
        inputFromServlet.close();
        instr.close();
        的System.out.println(1);
        返回结果;
    }
    赶上(IOException异常E)
    {
        的System.out.println(在messageServlet():+ E);
        msgBox.setText(msgBox.getText()+\\ n在messageServlet():+ E);
    }
    赶上(例外五)
    {
        的System.out.println(在messageServlet():+ E);
        msgBox.setText(msgBox.getText()+\\ n在messageServlet():+ E);
    }    返回null;
}公众的URLConnection connectToServlet()
{
    尝试
    {
        网址servletUrl =新的URL(获得codeBase的(),回声);
        康涅狄格州的URLConnection = servletUrl.openConnection();        conn.setDoInput(真);
        conn.setDoOutput(真);
        conn.setUseCaches(假);
        conn.setRequestProperty(内容类型,应用程序/ x-j​​ava的序列化对象);        康涅狄格州返回;
    }
    赶上(IOException异常E)
    {
        的System.out.println(在connectToServlet():+ E);
    }    返回null;
}


解决方案

您已经两个潜在的问题:


  1. Web服务器未启动。确保它已启动,并且的http://本地主机:8008 /环境/ servleturl 在你的网页浏览器工作正常


  2. 您使用了错误的URL。该 hxxp 方案是没有意义的。这是 HTTP



除了这一切,常见的做法是的的硬code在你的小程序中的基本域,这将使它不可移植(你需要修复/每次更改你的时候移动域)。刚刚从继承的 小程序#得到codeBase的() 方法。以此为基地,为你的servlet URL。

  URL的servlet =新的URL(获得codeBase的(),servleturl);
// ...

这里得到codeBase的()这样的回报喜欢的 HTTP://本地主机:8008 /背景


从所有这一切再次分开,我想preFER通过HTTP上面的Java特定的二进制数据发送纯文本,JSON或XML格式。最好是可重复使用,更容易pre /后处理。你有你想来回发送一个字符串一些字符。只需把它作为HTTP请求参数,让这个servlet通过的request.getParameter()等抓住它。为什么你会永远使用Java序列化呢?

I am sending a string from an Applet to a Servlet. When I go to get the output stream from the URLConnection I get the exception thrown java.net.UnknownServiceException: protocol doesn't support output

Some background info; I am using Eclipse, I tested the applet by running it in eclipse & in my own html page I made & they throw the same error. I have downloaded the correct Java Web SDK. Maybe I need to set up my hxxp://8008... server?

Why is this happening & how can I fix it? Do I need to sign my applet to make it work?

Here is my code & I have commented where the exception is thrown:

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;
}

解决方案

You've two potential problems:

  1. Webserver isn't started. Ensure that it is started and that http://localhost:8008/context/servleturl works fine in your webbrowser.

  2. You used the wrong URL. The hxxp scheme makes no sense. It's http.


Apart from this all, the common practice is to not hardcode the base domain in your applet, it would make it unportable (you would need to fix/change it everytime when you move domains). Just obtain it from the inherited Applet#getCodeBase() method. Use this as base for your servlet URL.

URL servlet = new URL(getCodeBase(), "servleturl");
// ...

Here getCodeBase() thus returns like http://localhost:8008/context.


Again apart from this all, I'd prefer sending plaintext, JSON or XML format over HTTP above Java-specific binary data. It's better reuseable and easier to pre/postprocess. You have some characters in a String which you'd like to send forth and back. Just send it as HTTP request parameter and let the servlet grab it by request.getParameter() and so on. Why would you ever use Java serialization for this?

这篇关于发送对象的Servlet抛出一个错误,我不能化解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆