How to pass array from jsp to servlet
I am trying to send some data from jsp to a servlet using an ajax call; In my java script, I have the following method:
function sendMessage(){
var cellPhones=[];
var title=$('#subject').val();
var text=$('#smsText').val();
var groupsName=$('#dg').datagrid('getSelections');
for(var i=0;i<groupsName.length;i++){
cellPhones.push(groupsName[i].cellphone);
}
alert(cellPhones);
$.ajax({
type:"POST",
url:"<%=basePath%>/SendMsgServlet?flag=sendSms",
data:{
title:title,
text:text,
cellPhones:cellPhones
}
}).done(function(){
alert("ok");
})
}
and in my doPost method I have:
if("sendSms".equals(flag.trim())){
String title=request.getParameter("title");
String text=request.getParameter("text");
String[] cellPhones=request.getParameterValues("cellPhones");
this.sendSms(title,text,cellPhones,request,response);
}
The problem is cellPhones is null, nut is not null, can anyone help me?
+3
source to share
4 answers
In ajax, you send values as an array like:
url= "cellPhone[]=121212&cellPhone[]=121212&cellPhone[]=121212"
But in a servlet, you are trying to get the value as a single value. You need to get the values as an array []
. Try,
String[] cellPhones=request.getParameterValues("cellPhones[]");
|- Use array sign
0
source to share