简体   繁体   中英

pass string variable from html file to JSP

I am trying to pass a string to a jsp file, and then I hope the jsp file will do something with the string. I don't know jsp though. I just want to make sure that my logic is correct. My code "works", but I want to make sure of what it does. I think I am posting the val of the string to browser's memory and calling the test.jsp file. Is that what is happening? Or should I do something completely different?

<!DOCTYPE html>

<html>
<head>
<title>Page Title</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
    $(document).ready(function(){
        var qString = '';
        $('#execute').click(function(){
        qString = $('#query-string').val();
        console.log(qString);
        $.post('test.jsp',qString);
        });
    });


</script>

</head>

<body>
<section>
    <label for="query-string">Enter your query here!</label>
    <input type="text" id="query-string">
        <button id="execute">Execute</button> 

</section>



</body>
</html> 

send your value as object and not string

 $(document).ready(function(){
    var qString = '';
    $('#execute').click(function(){
      qString = $('#query-string').val();
      console.log(qString);
      $.post('test.jsp',{qString:qString},function(data){
         //your stuff .. 
         alert('successfull');
      });
});

so this will post your values with a name qString to test.jsp page ..

{qString:qString} here key (qString) is the name by which the data is posted to jsp page (it can be anything) example {test:qString} , here data is posted as test so in jsp page you have to get the posted data by test ...

function(data){....}); this is a callback function which is called when your post method is successfully called and returns some data from the server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM