简体   繁体   中英

How to use html5 geolocation in the background for java web application

I have googled around attempting to find an answer to this question, but I have had no luck so I will ask it hear in the hopes that one of you can help!

I am attempting the use the javascript method navigator.geolocation.watchPosition to continuously gather location data.

The scenario is that I have a java web application using servlets and jsps, and I would like to begin geolocation collection when the user logs in, and stop when the user logs out. The data would be uploaded to a sql database after being collected. I have successfully been able to collect data for a single page, but I don't know how to implement a solution that works across all pages in the application.

I appreciate any and all help, thanks!

Since you are developing a Java web application, I would suggest

  1. Extract the JS logic that tracks user location from the JSP page, into a separate JS file or JSP file (if the script is generated dynamically).
  2. Use a Servlet filter to add a reference to this tracking JS file into all HTML pages returned to client. You may use Regular Expression to search in the HTML content, insert <script src="url to tracking JS file"> right before </head> tag.

The filter doesn't need to inject this script reference if it finds current user hasn't logged in yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM