1

I have a java file called myJavaFile.java. Inside this java file, I have the following java code:

private void addUploadscript() {

      //my java variable
    String fn = session.getUser();

    html.addElement("<script language=\"JavaScript\" type=\"text/javascript\">");
    html.addElement("function ajax_post(){");
    html.addElement("var hr = new XMLHttpRequest();");
    html.addElement("var url = \"http://localhost:8080/bunk/inf?cmd=Dialer.opts#\";");
    html.addElement("var vars = \"firstname=\"+fn;");
    html.addElement("\talert(vars)"); 
    html.addElement("hr.open(\"POST\",url,true);");
    html.addElement("hr.setRequestHeader(\"Content-type\",\"application/x-www-form-urlencoded\");");
    html.addElement("hr.onreadystatechange = function() {if (hr.readyState == 4 && hr.status == 200) {var return_data = \"my server is reached\";document.getElementById(status).innerHTML = return_data;} } ");
    html.addElement("hr.send(vars);");
    html.addElement("document.getElementById(status).innerHTML = \"processing....\";");
    html.addElement("}");
    html.addElement("</SCRIPT>");

}

I already tried to find my response by reading the other topics but I still can not resolve my problem. I actually tried to do it step by step and display my variables to make sure that I wrote everything fine.

Thank you very much for your help!

2
  • which variable ? fn ? If yes then just do this: html.addElement("var vars = \"firstname=\"+fn +"\;"); Commented Apr 20, 2016 at 19:32
  • Thank you very much for your reply Commented Apr 20, 2016 at 20:44

3 Answers 3

2

I believe you want to output fn directly, something like

html.addElement("var vars = \"firstname=" + fn + "\";");

Javascript allows you to use ' or " for String(s), and you could also use String.format(String, Object...). Like,

html.addElement(String.format("var vars = 'firstname=%s';", fn));
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for you reply, I corrected it. I actually am trying my "fn" java variable which is now "firstname" to a php application. This is my final code but I think I have another problem now, I do not know if "firstname' is sent or not. All I did is just put an alert in both cases (OK or non OK) but nothing is displayed. I also tried to set my "status" input type text on my java side but nothing is displayed. Please let me know if have an idea, I really appreciate that. Thank you.
Huh? Why are you involving Java then? You can make a ReST call to your PHP from Java or from Javascript. Anyway, your question appears to be about embedding JavaScript in Java.
Yes I am embedding javascript in Java. This is an existing application so I have to deal with it because everything is written in this way.
1

You should change this part of your code:

html.addElement("var vars = \"firstname=\"+fn;");

For this:

html.addElement("var vars = \"firstname=\"" + fn + "\";");

21 Comments

Thank you very much for you reply, I corrected it. I actually am trying my "fn" java variable which is now "firstname" to a php application. This is my final code but I think I have another problem now, I do not know if "firstname' is sent or not. All I did is just put an alert in both cases (OK or non OK) but nothing is displayed. I also tried to set my "status" input type text on my java side but nothing is displayed. Please let me know if have an idea, I really appreciate that. Thank you.
Can you post the generated HTML? If you are opening that page with Google Chrome, do you see any error in the developer console?
Hello Sanf, I tried to put the generated html but it says that it's too long. I have the generated html. Is there any over way to send it here?
Post only the generated script tag, just to check if there is any js error.
ajax_post({document.getElementById('buttonChat').style.display='none';var hr=new XMLHttpRequest();var url='localhost:9090/mibew/index.php/operator/login';var fn = document.getElementById('user_name').value; var vars = 'firstname='+fn;hr.open('POST',url,true);hr.setRequestHeader('Content-type','application/x-www-form-urlencoded');hr.onreadystatechange = function(){var return_data='';if (hr.readyState == 4 && hr.status ==200{return_data='ok';document.getElementById(status).innerHTML=return_data;}else{return_data='ko';document.getElementById(status).innerHTML =return_data;}}hr.send(vars);}
|
1

Just use single qoute ' instead of \" in all your code that will make it more cleanest, and replace this part :

html.addElement("var vars = \"firstname=\"+fn;")

By :

html.addElement("var vars = 'firstname='+fn;")

Hope this helps.

1 Comment

Thank you very much for you reply, I corrected it. I actually am trying my "fn" java variable which is now "firstname" to a php application. This is my final code but I think I have another problem now, I do not know if "firstname' is sent or not. All I did is just put an alert in both cases (OK or non OK) but nothing is displayed. I also tried to set my "status" input type text on my java side but nothing is displayed. Please let me know if have an idea, I really appreciate that. Thank you.

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.