Nitin
Nitin

Reputation: 149

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

I am trying to pass both latitude and longitude values from a Servlet to a JSP but I get only 1 value in the 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 am I going wrong?

Upvotes: 1

Views: 3748

Answers (4)

Nitin
Nitin

Reputation: 149

This is what i have done to store coordinates..

<% 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];
              }
%>    

with this can u provide me some help with the markers..

Upvotes: 0

Anurag Anand
Anurag Anand

Reputation: 498

You should pass Hashtable from servlet to 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);  
    }
%>

Upvotes: 1

Serge Ballesta
Serge Ballesta

Reputation: 148965

As currently written, you overwrite the 2 request attributes (lats and lon) at each iteration. A request attribute is not a magic container, but the simple object last used in addAttribute. So in you JSP, you later only get the value of last lats and lon.

Assuming lats and lon and arrays in your code, you 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 the arrays in the request attributes.

Then in the JSP ${lat} and ${lon} will refer to the arrays, and you can use ${lat[O]}, or ${lat[i]}, provided for last expression that i is a scoped variable containing a integer value less than array size.

Upvotes: 1

Md. Kamruzzaman
Md. Kamruzzaman

Reputation: 1905

Try like follows:

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

Upvotes: 0

Related Questions