2012-07-17 85 views
0

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

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的问题。

+0

@DarkXphenomenon我编辑我的文章。 – AnthonyMaia 2012-07-17 12:15:25

+0

更新问题的'output java:'部分,以显示发送新请求时会发生的情况。 – 2012-07-17 12:26:31

+0

@DarkXphenomenon输出没有改变。 – AnthonyMaia 2012-07-17 12:30:40

回答

3

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

在Java代码中,发回这样的事情

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

---data here--- 

Reference

喜欢的东西:

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

如何构建完整的HTTP响应? – AnthonyMaia 2012-07-17 12:04:28

+0

您至少需要添加'HTTP/1.1 200 OK',因为只有这样,JavaScript中的'readyState == 4'和您的响应中的'Content-Length'。 (或者你发送了一个分块响应,但我想这对你来说太复杂了。) – 2012-07-17 12:07:16

+0

@Birk什么是分块响应? – AnthonyMaia 2012-07-17 12:14:17

2

尝试:

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可能无法传送。

+0

谢谢,但是一直没有任何警报。 – AnthonyMaia 2012-07-17 12:17:08

+0

对不起,我目前在我的代码中有'Content-Length:13 \ r \ n \ r \ n +'。删除'+'。这是一个打字错误。在我的例子中,我改变了它。那么它应该工作。 – 2012-07-17 12:22:24

+0

这不是问题,我删除它,但一直没有。 – AnthonyMaia 2012-07-17 12:32:59