您的Servlet:
我正在尝试将位置数据从javascript发送到我的servlet,并及时将其存储在数据库中。我一直不知道如何访问我的参数?]
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);
}
}
我正在尝试将位置数据从javascript发送到我的servlet,并及时将其存储在数据库中。我一直不知道如何访问我的参数? NewFile.html
您的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"))); } }
我建议您关注naming conventions:
类名应为名词,每个内部单词的首字母应大写。尝试使您的类名称保持简单和描述性。使用整个单词,避免使用首字母缩写词和缩写词(除非缩写词比长格式(如URL或HTML)使用得更广泛)。
您的Servlet: