java - Passing multiple values from Servlet to JSP but I get only 1 value in JSP -


i trying pass both latitude , longitude values servlet jsp 1 value in jsp

servlet page

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 page

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);   } 

where going wrong?

as written, overwrite 2 request attributes (lats , lon) @ each iteration. request attribute not magic container, simple object last used in addattribute. in jsp, later value of last lats , lon.

assuming lats , lon , arrays in code, should write :

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); 

to put arrays in request attributes.

then in jsp ${lat} , ${lon} refer arrays, , can use ${lat[o]}, or ${lat[i]}, provided last expression i scoped variable containing integer value less array size.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -