Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8243601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:34:31+00:00 2026-06-07T21:34:31+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:34:32+00:00Added an answer on June 7, 2026 at 9:34 pm

    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());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use SSL to communicate between two components written in Java. I can't use
I try to communicate via TCP Socket between a QT4-Application (MyApp) and Cayuga (written
I am writing a networked application in Java, to communicate between the client and
I try to write a Facebook app. This app will communicate with a rfid
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
try { JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray.length();i++) { JSONObject json_data = jArray.getJSONObject(i);
Try executing the following in JavaScript: parseInt('01'); //equals 1 parseInt('02'); //equals 2 parseInt('03'); //equals
I am trying to generate on my Android app pkcs7 file to communicate between
I'm trying to communicate between my PC (Windows 7 using Netbeans and RXTX) with
I'd like to know how to communicate between processes on a Heroku worker dyno.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.