My JavaScript code sends coordinates to a Servlet for processing.
The JavaScript function retrieves coordinates from a JSP page like this:
function main1() {
$.ajax({
url: 'ServeltConnection',
type: "GET",
dataType: "json",
data: {
latitude: pos.lat,
longitude: pos.lng,
},
success: function(data){
//call of functions
}
});
}
I then pass these coordinates to my Servlet in order to receive a JSON response.
In my Servlet:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("application/json");
PrintWriter out = new PrintWriter(response.getWriter(), true);
List<Shop> shops;
try{
//code for connecting with database
shops=getShops(request.getParameter("latitude"),request.getParameter("longitude"));
String json = new Gson().toJson(shops);
response.getWriter().write(shops);
db.close();
} catch (Exception ex) {
out.println("Exception: " + ex.getMessage());
While I can successfully retrieve a JSON response using specific coordinates, I encounter issues when trying to obtain coordinates from the JS. I suspect that the problem lies in how I parse longitude and latitude values from the JS. I have looked into parsing JSON but have been unsuccessful so far. Can anyone provide guidance on how to resolve this issue?