1

How can I pass a var from javascript to a servlet. Yes I know you can use getParameter on the servlet side, but I first need to do some javascript stuff then from there it need to pass that new variable to servlet.

See my Javascript example:

function openBrWindowAndSubmit() { 

  var textBodyValue = encodeURIComponent(document.getElementById("textBody").value);
  alert(textBodyValue); //returns empty
  document.forms[0].submit();

  var textBodyValue = encodeURIComponent(document.getElementById("textBody").value);
  alert(textBodyValue); //works

}

As you can see in above example the first "alert" block returns empty, but the second one returns the correct encoded value because the document.forms[0].submit is already called. So is there a way I can get the second variable "textBodyValue" (which is outside of document.forms[0].submit) to the servlet? I'm calling this at the servlet side:

String test = req.getParameter("textBody"); 

Here's the JSP inside a form tag which calls the function on click:

 <textarea id="textBody" name="textBody"></textarea>        
 <input type="button" onClick="openBrWindowAndSubmit();" value="Click Here to Preview Email">

Is there any workaround to this problem?

I've been trying to change the javascript function to:

function openBrWindowAndSubmit() { //v2.0

  document.forms[0].target = "_blank";
  document.getElementById("action").value = "view_template";
  var textBodyValue = encodeURIComponent(document.getElementById("textBody").value);
  alert(textBodyValue);
  document.forms[0].submit();

  var textBodyValue = encodeURIComponent(document.getElementById("textBody").value);
  alert(textBodyValue);

  $.ajax({
      url: 'http://localhost:8080/restricted/comm/Email',
      data: textBodyValue,
    //  processData: false,
    //  contentType: false,
      type: 'POST',
      success: function(data){
        alert(data);
      }
    }); 
}

Can this work? i'm getting an undefined error when reaching the $ajax tag?

user2319262
  • 91
  • 5
  • 15
  • You could use a library like jQuery and override the submitfunction and then manually make a AJAX Post. – user Oct 15 '14 at 10:24
  • Could you please provide me with an example? – user2319262 Oct 15 '14 at 10:29
  • You have to include jquery in your .js file: – user Oct 15 '14 at 11:43
  • Thanks! I've added this and made error go away, but that ajax is not passing the new data to the servlet – user2319262 Oct 15 '14 at 12:06
  • data: textBodyValue, i dont think that this will work. Try var dataToSend = { textBodyValue : encodeURIComponent(document.getElementById("textBody").value)}; and then in your $.ajax data : dataToSend, – user Oct 15 '14 at 13:04

1 Answers1

0

As an idea. Dont know if its correct ;) but you can check it at the jQuery api page.

$('#idOfTheForm').on('submit', function(e){
    e.preventDefault();
    data = { key : val , key2 : val2 };
    $.ajax({
    type: "post",
    data: data,
    url : "",
    success : function(response){
                  console.log("return code was 200");    
               },
    error : function(jqXHR, textStatus, errorThrown){
                  console.log(errorThrown);
            }
    });
    return false;
}
user
  • 735
  • 1
  • 6
  • 21
  • Hi,Thanks but this is not right, can you try provide example as from my code? – user2319262 Oct 15 '14 at 11:09
  • @user2319262 This is your job. Maybe you can try yourself and update your question including error messages and so on – user Oct 15 '14 at 11:10
  • Thanks, Ive been trying to change the javascript function. Please see at my question above – user2319262 Oct 15 '14 at 11:36
  • what is your jsp page you are going to submit your request in your ajax call..? i think you can't use "http://localhost:8080/restricted/comm/Email" in the url... – Nomesh DeSilva Oct 15 '14 at 12:12
  • the page is http://localhost:8080/restricted/comm/Email , thats the servlet page where the new Var value needs to be send to – user2319262 Oct 15 '14 at 12:42