<%!
public String haversine(String lat1,String lon1,String lat2,String lon2)
  {
  int r = 6371;
  String val=null;
double dlat=((Double.parseDouble(lat2))-(Double.parseDouble(lat1)));
double dlon=((Double.parseDouble(lon2))-(Double.parseDouble(lon1)));
double a = (Math.sin(dlat/2)*Math.sin(dlat/2))+(Math.cos(Double.parseDouble(lat1))*(Math.PI/180))*(Math.cos(Double.parseDouble(lat2))*(Math.PI/180))*(Math.sin(dlon/2)*Math.sin(dlon/2));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = r * c;
if(d>1)
    val= ((d)) +"km";
else if(d < =1)
   val = ((d*1000)) +"m";
return val;
   }
%>

this is how i call the above function and print it

<%
                                       String hav = haversine(lat.substring(0, lat.indexOf(",")), lat.substring(lat.indexOf(",") + 1, lat.length()), abc.substring(0, abc.indexOf(",")), abc.substring(abc.indexOf(",") + 1, abc.length()));
                    %>
                    <td><font color ="white"><% out.println(hav);%></font>

i get a very wrong output for the above prog,,pls help,,

Recommended Answers

All 5 Replies

First of all, that is not proper way to write jsp.

A minor error is that in a jsp you never do this: <% out.println(hav);%> You do this: <%= hav %> And for the rest of the code this is how you need to proceed:
Create a separate class, called Utils for example, and put that method inside it.
Then display the input of the method at the page.

<%
String lat1 = lat.substring(0, lat.indexOf(","));
String lon1 = lat.substring(lat.indexOf(",") + 1, lat.length());
String lat2 = abc.substring(0, abc.indexOf(","));
String lon2 = abc.substring(abc.indexOf(",") + 1, abc.length());
%>

Lat1=<%=lat1%>, Lon1=<%=lon1%>, Lat2=<%=lat2%>, Lon2=<%=lon2%>

Take those inputs and run your method from a main method in order to debug it. After you are finished debugging and made sure that the method works then call it from the jsp.

What input do you pass at the method and what do you expect to receive? Add some System.out.println when debugging in order to see what values are generated.

thanx a ton javaaddict,,that was informative,,,but i keep getting a wrong value as distance,,,the 8th line u've posted is to get transfer it to javascript i suppose,,but the haversine function i've quoted above is a java function...

im passing 2 coordinates lat1,lon1,lat2,lon2 to the haversine function...thanx a lot for ur patient response,,,,and i still need it....is there anything wrong with that function????

That is java not javascript.
Also have you tried running that method in a main method. Not returning the right values has nothing to do with jsp. It has to do with the code in the method. So what values do you pass as arguments and what do expect it to do?

i input a set of coordinates,,,and i expect the distance between them. the coordinate of a point may be of the format 2.4454458393,78.39585839385 ,,,,,,,i still dont understand the 8th line u've posted,,,what is that if its not enclosed within scriptlet tags,,
or if im wrong pls explain me this line u've mentioned..

Lat1=<%=lat1%>, Lon1=<%=lon1%>, Lat2=<%=lat2%>, Lon2=<%=lon2%>

i input a set of coordinates,,,and i expect the distance between them. the coordinate of a point may be of the format 2.4454458393,78.39585839385 ,,,,,,,i still dont understand the 8th line u've posted,,,what is that if its not enclosed within scriptlet tags,,
or if im wrong pls explain me this line u've mentioned..

Lat1=<%=lat1%>, Lon1=<%=lon1%>, Lat2=<%=lat2%>, Lon2=<%=lon2%>

If you don't get the right value it means that your formula is wrong. What mathematical formula did you use for the method?

Also what kind of study did you do and started writing jsp pages? Did you read any books? Because if you don't understand that line, then stop immediately. Forget about jsp and start learning basic java. Then learn html and get a book about jsp and do some studying.

If you can't get that simple method to work then you shouldn't be working with jsp. So far all of my questions where about that method. What happens in the jsp is irrelevant. Try to debug and correct the method. Take some values and pass them to the method and see what it does. Also shouldn't that method take as arguments double values and return one double value?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.