0

I am trying to send a simple message from javascript. I want to receive the data in a java program (server), but I can't seem to figure out how to obtain the message in java.

Here is my 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 the above the fastest method to send simple string data from javascript to a java program or can you recommend an alternative solution?
  • How can I receive the above POST request from the javascript with java?

I really hope someone can help me out. Any help will be greatly appreciated

4
  • Are you using a Java server to host the webpage that the javascript is running in? Commented Apr 9, 2015 at 20:50
  • 1
    treat it exactly the same way as if it was a form being submitted. There are lots and lots of tutorials on how to use ajax Commented Apr 9, 2015 at 20:51
  • Thank your for your answers. I am not using java server. It is a javascript in Telerik Kendo UI. I managed to send a POST request to the java server, by doing as Jiří Kantor suggeested. However, I can't seem to extract the parameters sent with the POST request. So if any of you know how to do this, I would really appreciate your help. Commented Apr 10, 2015 at 18:56
  • JavaScript functions TO save a JSON file that can be read by java will do any good ? (basic javascript file saving)[jsfiddle.net/UselessCode/qm5AG/] Commented Apr 13, 2015 at 16:49

2 Answers 2

1

As I've never done something like this, I don't know if there is any fastest method, but this way the Java application has to listen on TCP port 80 and that should be it. It should also send a HTTP response.

EDIT: I have come up with two things after diving into the problem:

  1. The POST data part is not terminated by newline, so if you use BufferedReader and its readLine method, it just hangs there and waits. It doesn't get terminated because #2
  2. The AJAX request doesn't, apparently, close the socket, so the stream from which the java program reads is not terminated so you cannot end the reading by waiting for read method of InputStreamReader to return -1. The only way I found just this quickly was checking if the input stream is ready or not. So a VERY CRUDE but minimal example would 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("-------------------------");
            }
        }
    }
    
Sign up to request clarification or add additional context in comments.

2 Comments

This was what I was looking for! By listening on TCP, I received the POST request. But I can't seem to get the parameters, that I am sending with the request. Do you know how to extract these? Thanks you for your answer by the way!
Sorry for the messed up formatting, but I cannot get it to work :D
0

I think HttpServer is what you are looking for. It listens on port 80 to accept Http-Requests

HttpServer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.