0

I am trying to get array, which is created in javascript function to my java class.

In my java class this array comes as a null value.

here is my jsp code,

            <input type="button" value="Submit" onClick="submit()" />

                <script>
                    var a = "I am A"
                    var b = "I am B"
                    var c = "I am C"
                    var arr = [a,b,c];

                    function submit() {
                        $.ajax({

                            method : "POST",
                            url : 'myServlet',
                            dataType : "text/html",
                            data : {sts:arr}
                            success : function(resp) {
                            },
                            error : function(data) {
                            },
                        });
                    }

                </script>
            <!--  </form> -->
            </body>

            </html>

my java class to access the array,

            protected void doPost(HttpServletRequest request, HttpServletResponse response) {               
                String[] n=request.getParameterValues("sts");   
                System.out.println(n);
            }

also tried the following

            protected void doPost(HttpServletRequest request, HttpServletResponse response) {               
                String[] n=request.getParameterValues("sts[]");     
                System.out.println(n);
            }

both prints only null and not array.

Can anyone say where the mistake happens?

Thanks in advance.

1 Answers1

0

when you send arr in http request as data:{arr:arr}

check in developer console, it sending as

?arr%5B%5D=a&arr%5B%5D=b&arr%5B%5D=c

so try like this,

send arr as string

data:{arr:arr.toString()}

then in your java code get array like this,

String str = request.getParameter("arr");
String strA[] = str.split(",");

it is working....

Srinu Chinka
  • 1,471
  • 13
  • 19
Divesh Oswal
  • 250
  • 3
  • 13