简体   繁体   中英

how to send the array to servlet and save to the database

i had a function it contains an array.my problem is how to send the values which are in array to java.class file

this is my code

function sales_det(){

        var salDet=document.getElementsByClassName("sales_details");
        //console.log(salDet.length);
        for (var i = 0; i < salDet.length; i++) {
            var sal=salDet[i].getElementsByTagName("td");
            window["array_"+i] = [];
 window["array_"+i].push(sal[0].textContent,sal[1].textContent,sal[2].textContent,sal[3].textContent);

           console.log(window["array_"+i]);
        }

    }  

you can store it as attribute in some scope. and retrieve it in servlet. like this way

request.setAttribute("myarray",arrayObject);

request.getAttribute("myarray"). // here not forget to caste it. because it store as Object only

您可以创建一个用逗号分隔的数组值字符串,使用request.getParameter获取它,然后使用strObj.split(“,”)在servlet中拆分它

使用表单提交或ajax调用将数组与请求一起传递,然后在服务器端使用以下代码检索值:

String array[] = request.getParameterValues("arrayName");

You can try this:

  1. create a function in js that returns your array.

    function arrayFromJS(){ //return your array }

  2. from html

    //input type="hidden" name="arrayfromjs" value=arrayFromJS()

  3. in servlet you can use:

    request.getParameter("arrayfromjs");

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