9

I have looked previous questions on this topic on SO, but my problem is not solved yet.

I am passing the array from javascript to servlet.

JavaScript Code:

var action = new Array();
function getProtAcionValues(rowNo,columnCount)
{
    for(var j=0;j<columnCount;j++)
    {
        action[j] =  document.getElementById('textActions'+rowNo+''+j).value;
        alert(action[j]);
    }
}

Servlet Code:

String actions[] = request.getParameterValues("action[]");
if(actions!=null)
for(int i=0;i<actions.length;i++)
{
    System.out.print(" Action: "+actions);
}
else
    System.out.println("Action is null");

Using above code I am getting message "Action is null".

And if I try

String actions[] = request.getParameterNames("action[]");

I am getting Syntax error:

The method getParameterNames() in the type ServletRequest is not applicable for the arguments (String)

Please let me know if there is something wrong in code.

4
  • Where is the HTML or JavaScript code that calls the servlet? Commented Dec 31, 2012 at 8:55
  • Can you post the code of how are you sending the action array to the servlet... Commented Dec 31, 2012 at 8:56
  • <form name="form2" method="post" action="ServletName"> <input type="submit" value="Update" id="btnUpdate" onclick="passValues()" name="btn"> Commented Dec 31, 2012 at 9:03
  • Can you post the code of how are you sending the action array to the servlet.i mean how to set action[] to request object,please let me know it is very useful for me Commented Apr 2, 2014 at 5:07

3 Answers 3

7

you can just simply get the array with the name of the array...

String actions[] = request.getParameterValues("action");

Sign up to request clarification or add additional context in comments.

1 Comment

Tomcat isn't recognizing the parameter unless I add brackets to the end of the name, i.e. ("action[]")..
2

You can't pass a java array as a parameter, as it is an structure. The best way is to serialize it into an string object like a jSon. You can use JSON.stringify. Simple and efficient. As you can serialize in the server also, it's very useful.

Comments

0

Pass Javascript array variable with form action to send values to servlet, and then use

String[] darray=request.getParameterValues("variable name used with link");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.