client:
Try {<br/> // establish a connection <br/> URL url = new URL ("servlet address"); <br/> urlconnection con = URL. openconnection (); </P> <p> con. setusecaches (false); <br/> con. setdooutput (true); <br/> con. setdoinput (true); <br/> con. setrequestproperty ("Content-Type", "application/octet-stream"); </P> <p> // send data <br/> hashmap map = new hashmap (); <br/> map. put ("test", test); <br/> objectoutputstream objoutstream = new objectoutputst Ream (con. getoutputstream (); <br/> objoutstream. writeobject (updateppphashmap); <br/> objoutstream. flush (); <br/> objoutstream. close (); </P> <p> // read data <br/> objectinputstream objinstream = new objectinputstream (con. getinputstream (); <br/> string message = (string) objinstream. readobject (); <br/> If (message. equals ("success") {<br/> joptionpane. showmessagedialog (this, "submitted successfully! "); <Br/>}else {<br/> joptionpane. showmessagedialog (this," submission failed! "); <Br/>}< br/> objinstream. close (); <br/>}catch (malformedurlexception e) {<br/> E. printstacktrace (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>} catch (classnotfoundexception e) {<br/> E. printstacktrace (); <br/>}
Note:
If the client only has an output stream and no input stream, the connection cannot be triggered. The reason is unknown.
Server:
Try {<br/> // read data <br/> objectinputstream OOS = new objectinputstream (super. getrequest (). getinputstream (); <br/> hashmap map = (hashmapoos. readobject (); <br/> OOS. close (); <br/> // send data <br/> objectoutputstream out = new objectoutputstream (super. getresponse (). getoutputstream (); <br/> out. writeobject ("success"); <br/> out. flush (); <br/> out. close (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}catch (classnotfoundexception e) {<br/> E. printstacktrace (); <br/>}