XMLHttpRequest java javascript

I try to communicate between javascript and java. My script javascript send a message to java and java send a response.

javascript part:

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4)
        {
        alert(xmlhttp.responseText);
        }
      }
    var s = "LIGNE \n 2 \n il fait beau \nEND\n";
    xmlhttp.open("POST","http://localhost:6020",true);
    xmlhttp.send(s);

java part:

    try {
        serverSocket = new ServerSocket(6020);
    } catch (IOException e) {
        System.err.println("Could not listen on port: 6020.");
        System.exit(-1);
    }
    serverSocket.accept()
    BufferedReader br = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
    BufferedWriter bw =  new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream()));
    String ligne = "";
    while(!(ligne = plec.readLine()).equals("END")){
        System.out.println(ligne);
    }
    bw.write("Il fait beau\n");
    bw.flush();
    bw.close();
    plec.close();
    socket.close();

output java :

POST / HTTP/1.1
Host: localhost:6020
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://localhost:8080/test.html
Content-Length: 30
Content-Type: text/plain; charset=UTF-8
Origin: http://localhost:8080
Pragma: no-cache
Cache-Control: no-cache

LIGNE 
 2 
 il fait beau 

So, I receive correctly the message send by javascript but the alert his always empty. How to response at this message? I try a lot of possiblity but they don't work. And I don't want to use the servlet, it's to heavy to do that.

Thanks.

Edit:

I did this :

bw.write("HTTP/1.1 200 OK\r\n"+
        "Content-Type: text/html; charset=utf-8\r\n"+
        "Content-Length: 13\r\n\r\n" +
        "il fait beau\n");

and this:

String data = "il fait beau \n";

StringBuilder builder = new StringBuilder();

builder.append("HTTP/1.1 200 OK\r\n");
builder.append("Content-Type: text/html; charset=utf-8\r\n");
builder.append("Content-Length:" + data.length() + "\r\n\r\n");
builder.append(data);
bw.write(builder.toString());

But the alert remain empty. Maybe it's a problem in the javascript.

Answers


The javascript needs to see a full HTTP response. Merely sending back characters to it, makes it discard the reply as it is an invalid HTTP response.

In your java code, send back something like this

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: <length of data>

---data here---

Reference

Something like:

StringBuilder builder = new StringBuilder();
builder.append("HTTP/1.1 200 OK\r\n");
builder.append("Content-Type: text/plain; charset=utf-8\r\n");
builder.append("Content-Length:" + data.length() + "\r\n\r\n);
builder.append(data);
bw.write(builder.toString());

Try:

bw.write("HTTP/1.1 200 OK\r\n"+
            "Content-Type: text/html; charset=utf-8\r\n"+
            "Content-Length: 13\r\n\r\n" +
            "il fait beau\n");

HTTP-Headers are separated by \r\n (CRLF). Headers and body is spearated by \r\n\r\n.

Note that you set the length to 13 because you also have to count the \n at the end of your string.

EDIT: It does not work because of the cross-domain-policy. http://localhost:6020 is not the same port as the website which executes your JavaScript and so the xmlhttprequest might not be delivered.


Need Your Help

Call C function in C++ class implementation

c++ cmake clion gumbo

I have to use https://github.com/google/gumbo-parser library that is written in C.

write a text file using PHP to a specific URL of the same server

php file text

I have a php script that writes a text file to the same directory where the script is.