Communication between Applet and Servlet

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.