princess
princess

Reputation: 31

How to access a variable used in java script from jsp page?

I am using ajax in my web page.I want to access a variable used in java script function (in head section of html) by a jsp page.Jsp page is retrieving data from database using that variable.

How can i do that?

Please help me.

Upvotes: 3

Views: 1346

Answers (4)

StaticVariable
StaticVariable

Reputation: 5283

You can simply do this by making a ajax call and passing that variable with ajax request

<script>
var id=1;

$.get("yourpage?id="+id,function(){

//get this id serverside using `get` 
})

</script>

Upvotes: 1

Chakravarthi Bharathi
Chakravarthi Bharathi

Reputation: 234

You have to change your approach to get this done. We cannot set javascript variable value to the the database code which is written in the jsp file. This is because, the database code will be rendered from the serverside and only the html will be sent to the client.

You can achieve this by having a Controller(Servlet) with this database code with asynchronous support and in your jsp file, call this controller through javascript ajax and manipulate the HTML DOM for your requirement.

Upvotes: 0

Naresh J
Naresh J

Reputation: 2137

You can do it by adding new jsp file, flow will be like this :

  1. Create new jsp file like databaseOperation.jsp which will contain your code for retrieving database record.
  2. Through javascript function, call databaseOperation.jsp file and pass your javascript varible.

  3. Access this variable in your jsp file and retrieve the required code from DB.

Upvotes: 1

Lewis E.
Lewis E.

Reputation: 337

if you need the call data you will set & or have a var that's empty before hand. what I realized is that ajax localizes even declared new var's the best way i've found is to append to a existing. Now with the new string and or literal array / object

 <script>
     function s(e){
      alert(e);
     }

      var a = '';

    //Jquery Version
     $.get('test.php',function(data){
       a += data;
      s(a);
      });

    // Javascript Version
      function ajaxFunction(){
        var ajaxRequest;  // The variable that makes Ajax possible!

        try{
            // Opera 8.0+, Firefox, Safari
            ajaxRequest = new XMLHttpRequest();
        } catch (e){
            // Internet Explorer Browsers
            try{
                ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try{
                    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e){
                    // Something went wrong
                    alert("Your browser broke!");
                    return false;
                }
            }
        }
        // Create a function that will receive data sent from the server
        ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){

                a += ajaxRequest.responseText;
                s(a);
            }
        }

        ajaxRequest.open("GET", "test.php" , true);
        ajaxRequest.send(null); 
    }

    $(function(){
    ajaxFunction();
    });
    //-->
    </script>

Upvotes: 2

Related Questions