How to send simple data from javascript to java application -


i trying send simple message javascript. want receive data in java program (server), can't seem figure out how obtain message in java.

here javascript:

   if (window.xmlhttprequest) {         xmlhttp = new xmlhttprequest();     } else {         xmlhttp = new activexobject("microsoft.xmlhttp");     }      xmlhttp.open("post", ip, true);     xmlhttp.setrequestheader("content-type", "application/x-www-form-urlencoded");     xmlhttp.send("posx=" + posx + "&posx = " + posx); 

i have 2 questions regarding this.

  • is above fastest method send simple string data javascript java program or can recommend alternative solution?
  • how can receive above post request javascript java?

i hope can me out. appreciated

as i've never done this, don't know if there fastest method, way java application has listen on tcp port 80 , should it. should send http response.

edit: have come 2 things after diving problem:

  1. the post data part not terminated newline, if use bufferedreader , readline method, hangs there , waits. doesn't terminated because #2
  2. the ajax request doesn't, apparently, close socket, stream java program reads not terminated cannot end reading waiting read method of inputstreamreader return -1. way found checking if input stream ready or not. crude minimal example be:

    import java.io.*; import java.net.*;   class tcpserver {       public static void main(string argv[]) throws exception {         serversocket welcomesocket = new serversocket(80);          while(true) {             socket connectionsocket = welcomesocket.accept();             inputstreamreader infromclient = new inputstreamreader(connectionsocket.getinputstream());             int c = 0;              while(true) {                 c = infromclient.read();                 system.out.print((char)c);                 if(!infromclient.ready()) {                     break;                 }             }             system.out.println("-------------------------");         }     } } 

Popular posts from this blog