0

I have an application where i am getting some values in the console in the form of a javascript array.now i want to send the values to the servlet.My javascript array look like this ..

0.Java
1.Perl
2.C#

This values i am getting in the console.i have to send those values to the Servlet.But i can not do that.I am able to send multiple values to the Servlet,but dont know how to send array of javascript.The way i send multiple values to the Servlet is

 $.ajax({
        url: "AccountServlet",
        type: "post",

        dataType: "json",
        data: { subject1:java,subject2:perl..etc},
        error:function(){
            //alert("error occured!!!");
        },
        success:function(data){
            alert(data.fullName + "\n" + data.mobileNo);
        }
     });

and in the Servlet i catch them

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String sub1=request.getParameter("subject1");
            String sub2=request.getParameter("subject2);
    ;

Like this but can anyone help me how to store array of javascript here. }

1 Answer 1

2

There are several ways to handle this, but the easiest would be to pass the array as one parameter in your javascript code and then retrieve it as a string in your Java code. use a JSON library like Gson or Simple JSON to parse the array from the string into a native Java array.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

Gson gson = new Gson();
String[] arr = gson.fromJson(request.getParamenter("subjects"), String[].class); 
 // ... do more here
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.