0

Good Evening, I try to do a checkbox which embedded in a JSP page. I use document.getElementByID("checkbox") in the JSP javascript function. How to pass the variables in the javascript function to another java file without passing it through url for security concern?

This is Checkbox Function:

var checkbox = document.getElementById("chbx");

 function foo(){
   if(checkbox.checked=true){
       //passThisVariableToAnotherJavaFile-isChecked
         }
   else {
        //passThisVariableToAnotherJavaFile-isNotChecked
         }
    };

This is Java File:

public class CheckBoxEvent{

if(isChecked) {
   //then whatever
} else if (isNotChecked) {
          //then no whatever
}

}

I am a newbie is this jsp stuff, I used to be doing this in PHP but everything mixed-up in my mind when there is a HashMap appear in the java file. Well, need some hints and help.

Thank You

Michael
  • 15,386
  • 36
  • 94
  • 143
薛源少
  • 306
  • 1
  • 6
  • 18
  • 1
    The only way to realistically implement something like this that I can see is with AJAX. – Jordan Denison Jul 21 '13 at 15:07
  • thanks for the reply, I will look towards Ajax and JQuery... – 薛源少 Jul 21 '13 at 15:40
  • @薛源少, Buddy, understand this, your whole javascript part is on the client side (browser), the java code, jsp, servlets are on server side ( running in app servers ).. so there's no way your javascript can directly use java code.. you have to either make a GET OR POST request.. my simple suggestion is send a GET request to some servlet as "window.location = './CheckBoxServlet?checked=true';" ... – Dreamer Jul 21 '13 at 15:51
  • I going to use ajax to get the java function and by passing the checkbox variable into it. with the help of a servlet, thanks friends.now I can differentiate whether jsp or java is server/client side language...thanks a lot... – 薛源少 Jul 21 '13 at 16:07

2 Answers2

1

How to pass the variables in the javascript function to another java file without passing it through url for security concern?

You have two options here and in both the cases you need to send the value to the server for processing :

  1. An AJAX POST or GET request. This looks more appropriate to your requirement. You can get an example here.
  2. Submit the form using POST.

Read here When do you use POST and when do you use GET?

In both the cases , there will be a Servlet/JSP/Controller handling the request in the server. You can call the methods in your Custom Java class with the request parameters.

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • why only POST. It can be GET also. Any specific reason for mentioning POST? – M Sach Jul 21 '13 at 15:13
  • thanks, the example really matches with my requirements... now I going to pass the java function back to the jsp with the request.(XXX), just for certainty. In this cases, if there is a database query involved where do u placed them? Jsp or Java? Just for the opinion to be refer, its ok if u dun wan to answer... thanks a lot... – 薛源少 Jul 21 '13 at 15:39
  • You should have a separate DAO layer for that . Write the DB access code in Java file. – AllTooSir Jul 21 '13 at 15:49
0

when you do var checkbox = document.getElementById("chbx"); you are doing it at client side. Now if you want to propagate this value to server side i.e to some java class(most of the cases it will be servlets) then you have two options:-

1)Make an AJAX call. (You can also use jquery here to make AJAX call).Here is 

the small example, you can find ample on google

$.ajax({ 
type: 'GET', 
dataType: 'json', 
url: servlerURL, 
success: function(reply) { 

},
error: function (xhr, textStatus, errorThrown) { 

} 

});

or

2)Submit the form
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • what u mean is implement the ajax in the javascript funtion like ajax.call("Java.file"); then how it will pass the variables to the java file? public isChecked(isChecked){this.isChecked = isChecked; return isChecked;} Can provide some examples? – 薛源少 Jul 21 '13 at 15:17