Store a java script value in jsp String variable

17,103

Server-side vs Client-side

In order to allow your server to respond to user input there has to be a request sent. You can ask your server can send to send a response in one of two ways:

  1. A completely new page - such as when a link is clicked and a new HTML page loaded
  2. A AJAX response - run asynchronously and formatted however you want so that your client side JavaScript can use it.

But the server cannot manipulate the DOM of your existing page by itself. Even with AJAX, it has to have some code (usually JavaScript) on the client side to manipulate the page.

Normally if you are doing a search, I would recommend option 1. This has the benefit of being simpler and it allows your users to navigate back/forwards or bookmark their search (assuming you make a GET call vs POST). See When to Use AJAX and When Not To

Using AJAX

If you want to use AJAX, make a call to your server create your xmlhttp object and then then you can pass your variable as @A4L mentioned only using AJAX. It would look something like this:

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    // Do what you want with the results from your server
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
var xmlhttp = new XMLHttpRequest();

xmlhttp.open("GET",'myJSP.jsp/?id=' + id,true);
xmlhttp.send();

Then your server can pull in the parameter like @A4L mentioned. You may want to have your response just be straight text or an HTML block that the javascript can place inside a DIV but that's all up to you.

Disclaimer: I'm no expert in AJAX. Most of this is something you can learn from the W3 Schools site

Share:
17,103
Vijay Kumar Khushlani
Author by

Vijay Kumar Khushlani

Updated on June 04, 2022

Comments

  • Vijay Kumar Khushlani
    Vijay Kumar Khushlani almost 2 years

    Can anyone help me to solve my simple problem of my jsp code? here is my jsp page with JavaScript

    function fid()
    {
        var id=document.getElementById("id").value;
        alert(id);
        <%
                 String demo=id;             // store id value of java script but not working
                 or find(id);                // or call a function with parameter of java script var value
                                           // both are not working
         %>
    }
    

    here is my html page --

    Enter Student Id<input type='text' id='id'><button type='button' onclick='fid()'>Find</button>
    

    How do I pass the js value in jsp function.
    I know that I can do this with ajax or jquery but I don't know these languages. Any suggestions or code snippets are welcome.

  • Vijay Kumar Khushlani
    Vijay Kumar Khushlani about 11 years
    no . bro i need to do this on some page . pls give me ajax code
  • Vijay Kumar Khushlani
    Vijay Kumar Khushlani about 11 years
    hmm. can u give me JQuery code . and pls tell me how to use that code . i just wonted to my work . doesn't mettar how it's work .
  • Vijay Kumar Khushlani
    Vijay Kumar Khushlani about 11 years
    as i said that bro i need on same page . give me ajax or JQuery code for that