XMLHttpRequest java javascript

XMLHttpRequest java javascript

本文关键字:javascript java XMLHttpRequest      更新时间:2023-10-01

我尝试在javascript和java之间进行通信。我的脚本javascript向java发送消息,java发送响应。

javascript部分:

    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部分:

    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();

输出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 

因此,我正确地接收到javascript发送的消息,但警报总是空的。如何对此消息作出回应?我尝试了很多可能性,但都不起作用。我不想使用servlet,这样做太重了。

谢谢。

编辑:

我做到了:

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");

这个:

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());

但警报仍然是空的。也许这是javascript中的问题。

javascript需要看到完整的HTTP响应。仅仅向它发回字符,就会使它丢弃回复,因为它是一个无效的HTTP响应。

在您的java代码中,发送类似以下的内容

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

参考

类似于:

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());

尝试:

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标头由'r'n(CRLF)分隔。头部和主体由'r'n'r'n插入。

请注意,您将长度设置为13,因为您还必须计算字符串末尾的'n

编辑:由于跨域策略,它不起作用http://localhost:6020与执行JavaScript的网站不是同一个端口,因此可能无法传递xmlhttprequest。