Jim
Jim

Reputation: 81

Sending object to Servlet throws an error I cant resolve

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

Upvotes: 1

Views: 1129

Answers (1)

BalusC
BalusC

Reputation: 1108742

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?

Upvotes: 1

Related Questions