1

i am trying to send the location data from javascript to my servlet and to store it in a data base at a timely basis.i am keep getting null how to get access of my params?

NewFile.html

<script>
    $(document).on("click", "#somebutton", function() {
        if (navigator.geolocation) {

            navigator.geolocation.watchPosition(showPosition);
        } else {
            x.innerHTML = "Geolocation is not supported by this browser.";
        }

        function showPosition(position) {
            console.log(position)

            var params = {
                lat : position.coords.latitude,
                lng : position.coords.longittude
            };

            $.get("someservlet", $.param(params), function(responseText) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
                // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
            });

        }

    });
</script>

servlet.java

public class servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String text = "some text";
       String params= request.getParameter("params");
        System.out.println(params);
    }

}

  • You probably want to use Post instead of Get, not sure that it is main reason of why you get Null value but start with that. Then check from browser network tools what data and how actually send to back-end. I am not familiar with java servlets so unable to give an answer just those little tips. – maximelian1986 Jan 10 '20 at 10:23
  • Thanks @maximelian1986 but i am using get method in servlet also so no probs on that side! i just need to access the info after getting it – gopal krishna mareti Jan 10 '20 at 10:27

1 Answers1

2

Your Servlet:

public class servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String text = "some text";
        System.out.println(String.format("Lat: %s Long: %s", request.getParameter("lat"), request.getParameter("lng")));
    }

}

I suggest you to follow naming conventions:

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

Renato
  • 2,077
  • 1
  • 11
  • 22