java - Sending object to Servlet throws an error I cant resolve -


i sending string applet servlet. when go output stream urlconnection exception thrown java.net.unknownserviceexception: protocol doesn't support output

some background info; using eclipse, tested applet running in eclipse & in own html page made & throw same error. have downloaded correct java web sdk. maybe need set hxxp://8008... server?

why happening & how can fix it? need sign applet make work?

here code & have commented exception 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 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 2 potential problems:

  1. webserver isn't started. ensure started , http://localhost:8008/context/servleturl works fine in webbrowser.

  2. you used wrong url. hxxp scheme makes no sense. it's http.


apart all, common practice not hardcode base domain in applet, make unportable (you need fix/change everytime when move domains). obtain inherited applet#getcodebase() method. use base servlet url.

url servlet = new url(getcodebase(), "servleturl"); // ... 

here getcodebase() returns http://localhost:8008/context.


again apart all, i'd prefer sending plaintext, json or xml format on http above java-specific binary data. it's better reuseable , easier pre/postprocess. have characters in string you'd send forth , back. send http request parameter , let servlet grab request.getparameter() , on. why ever use java serialization this?


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -