Я пытаюсь передать значения широты и долготы от сервлета к JSP, но я получаю только 1 значение в JSP
Страница сервлета
for(int i=0;i<json.length();i++)
{
String lat=json.getJSONObject(i).get("lat").toString();
String lon=json.getJSONObject(i).get("lon").toString();
lats[i]=lat;
lons[i]=lon;
request.setAttribute("lats", lats[i]);
request.setAttribute("lons", lons[i]);
System.out.println(lats[i]+","+lons[i]);
}
Страница JSP
var len=<%=request.getAttribute("len")%>;
lats[0]=<%=request.getAttribute("lats")%>;
<% String[] lats=(String[]) request.getAttribute("lats");%>
<% String[] lons=(String[]) request.getAttribute("lons");%>
for(i=0;i<len;i++)
{
var locations =[
['<%=request.getAttribute("cid")%>',lats,lon]
];
alert(locations);
}
Где я иду не так?
Как и в настоящее время, вы перезаписываете 2 атрибута запроса (lats
и lon
) на каждой итерации. Атрибут request не является волшебным контейнером, а простым объектом, который используется последним в addAttribute
. Таким образом, в вас JSP, вы позже получаете только значения последних lats
и lon
.
Принимая lats
и lon
и массивы в вашем коде, вы должны написать:
for(int i=0;i<json.length();i++)
{
String lat=json.getJSONObject(i).get("lat").toString();
String lon=json.getJSONObject(i).get("lon").toString();
lats[i]=lat;
lons[i]=lon;
System.out.println(lats[i]+","+lons[i]);
}
request.setAttribute("lats", lats);
request.setAttribute("lons", lons);
Поместить массивы в атрибуты запроса.
Затем в JSP ${lat}
и ${lon}
будут ссылаться на массивы, и вы можете использовать ${lat[O]}
или ${lat[i]}
, при условии последнего выражения, что i
- переменная с областью содержащий целое значение меньше размера массива.
Вы должны передать Hashtable из сервлета в JSP.
Servlet
HashMap latsMap = new HashMap();
HashMap lonMap = new HashMap();
for(int i=0;i<json.length();i++)
{
String lat=json.getJSONObject(i).get("lat").toString();
String lon=json.getJSONObject(i).get("lon").toString();
lats[i]=lat;
lons[i]=lon;
latsMap.put("lats"+i,lats[i]));
lonMap.put("lons"+i,lons[i]));
System.out.println(lats[i]+","+lons[i]);
}
//You can put these as Session attribute also
request.setAttribute("lats", latsMap);
request.setAttribute("lons", lonMap);
JSP
<% HashMap latsMap==(HashMap)request.getAttribute("lats");
HashMap lonMap=(HashMap)request.getAttribute("lons");
int len = latsMap.size();
for(int i=0;i<len;i++)
{
String lats = latsMap.get("lats"+i);
String lon= lonMap.get("lons"+i);
String locations ="[['"+request.getAttribute("cid")+"',"+lats+","+lon+"]]";
//If request.setAttribute("cid",<SomeValue>); is not present in servlet then
//remove request.getAttribute("cid") from JSP , Change it to
//String locations ="[,"+lats+","+lon+"]]";
out.println(locations);
}
%>
cid
в сервлете, если нет, то удалите часть request.getAttribute("cid")
и он должен стать. String locations ="["+lats+","+lon+"]"
. Я обновил свой код с помощью String lon= lonMap.get("lons"+i);
Это то, что я сделал для хранения координат.
<% Integer len =(Integer)request.getAttribute("len");
int ilen =len.intValue();
String[][] locations =new String[ilen][3];%>
<% String[] lats=(String[]) request.getAttribute("lats");
String[] lons=(String[]) request.getAttribute("lons");
for(int i=0;i<ilen;i++)
{
System.out.println("i = "+i+", latlong= "+lats[i]+","+lons[i]);
locations[i][0] = (String) request.getAttribute("cid");
locations[i][1] = lats[i];
locations[i][2] = lons[i];
}
%>
с этим я могу помочь мне с маркерами.
Попробуйте следующее:
for(int i=0;i<json.length();i++) { String lat=json.getJSONObject(i).get("lat").toString(); String lon=json.getJSONObject(i).get("lon").toString(); lats[i]=lat; lons[i]=lon; System.out.println(lats[i]+","+lons[i]); } request.setAttribute("lats", lats[i]); request.setAttribute("lons", lons[i]);