-3

What do I need to research, know and use for the following project:

I want to use javascript to show a map, using googlemaps api, on a webpage. It will have markers on it. The markers will be geolocations using latitude and longitude. The lat and lon coordinates will be found by entering IPs into ip-api.com which will give me lat and lon. The IPs are taken from a database. This map will also be displayed in a jpanel in a Java program.

My biggest concern and confusion is with how I will connect to the DB and get the data. I have no experience with javascript but I heard that it is bad practice to connect to a DB in javascript on the webpage. Is it possible to connect, retrieve, and push data using my java program as the mediator between the DB and webpage? It seems that this would keep data safe but I don't know how I would, or if its possible, to send data from java to the script.

Perhaps someone has other suggestions?

UPDATE I initially asked this question with only a general sense of direction and a muddled understanding merely looking for a point in the right direction. Instead of downgrading thinking my question was stupid or that I didn't try finding an answer in the first place, the answers below actually took time to help and were able to guide me to where I needed to go. Next time don't be so quick to downgrade such a clearly inexperienced programmer and attempt to lend a hand. That's what this community is for and why I love using it.

Roman C
  • 49,761
  • 33
  • 66
  • 176
exit_1
  • 1,240
  • 4
  • 13
  • 32
  • Yes, this is a way the JSP system manifests itself. – nanofarad Aug 03 '13 at 03:09
  • could your perhaps provide any documentation that will help me in understanding this process @hexafraction? thanks for your input – exit_1 Aug 03 '13 at 03:10
  • no need to downgrade. I don't have a strong programming background. I'm simply trying to learn – exit_1 Aug 03 '13 at 03:12
  • What have you tried/found so far? We need to know if anything didn't work so we could address it *specifically*. I personally didn't downvote this post. – nanofarad Aug 03 '13 at 03:12
  • Well so far I have a similar program constructed using strictly java but I only use google static maps by constructing a url request. I want to use googlemaps api in javascript which will allow me to use the MarkerClusterer. In order to do this without violating terms of usage, this api has to be publicly accessed, hence the integration with a web page. – exit_1 Aug 03 '13 at 03:17
  • I guess I'm just looking for experienced programmers to see what i want to do, tell me what allows me to do that, and point me int he direction that will allow me to learn and do this myself – exit_1 Aug 03 '13 at 03:19
  • +1, perfectly legitimate question. It feels like you are just getting started and just need a push in the right direction. – hatcyl Aug 03 '13 at 03:56

4 Answers4

2

JavaScript can't directly connect to a database that's living on another server; there's just no API to do it (except maybe WebSockets). What you probably want to do is to use AJAX in your JavaScript and write an application for a Web server that answers AJAX requests with answers from the database.

This is a complicated task and will require a good bit of learning; depending on your situation it may be more practical to get someone else to write the server part while you write the HTML/JavaScript part. If you do want to take the time to learn it all yourself, I recommend starting with Spring MVC, since it makes it very easy to write JSON-based Web services.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
2

Yes, Java can do this. So can Python, PHP and most other languages. Using javascript to directly access a database from a web page is problematic for security reasons. Without knowing more about the problem, the constraints or your experience with Java it's difficult to provide clearer direction. If all of this is new to you then I will say you have a non-trivial amount of work to learn how to do this. For interacting with a database from Java, the following may be helpful:

http://docs.oracle.com/javase/tutorial/jdbc/

Here's a series of video tutorials using Java to create Rest services that javascript can interact with via Ajax:

http://www.youtube.com/watch?v=gKBiIWY7FYw

http://www.youtube.com/watch?v=4DY46f-LZ0M

http://www.youtube.com/watch?v=LcZSty50KTw

Benjamin Pack
  • 365
  • 2
  • 3
  • Thanks, I'm familiar with java to DB interaction. The problem is that now i have to introduce javascript into the equation which I don't have experience with. I'll watch these videos, thanks for the links – exit_1 Aug 03 '13 at 03:30
1

Yes, you can use server-side Java code as a mediator. Use JavaScript to POST data to an HttpServlet, via JavaScript's XMLHttpRequest object. Then handle the data in your servlet.

Once you've done your DB business, you can send a "All done!" response back, to be handled by your JS.

I suggest reading up on the XMLHttpRequest object. Also, look into HttpServlet examples involving POSTed data.

Here's a quick example:

JS (Works in IE9+)

var xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function(data) {
  console.log(data);
};
xmlhttp.open("POST", "/servlet", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");

Java

public class MyServlet extends HttpServlet {
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {

    String fname = req.getParameter("fname");
    // db code here

    PrintWriter out = resp.getWriter();
    out.print("All done!");
  }
}
Jackson
  • 9,188
  • 6
  • 52
  • 77
0

Here's my thoughts:

  1. You want to store IPs for some reason. You use a database. (You got that part.)

  2. You want to read from that database. You do that with "Server Side" code. Such as PHP, ASP, or JSP (and others).

  3. You want to display info in your WebPage and Java desktop program. You do that with Javascript and well Java.(you also got that part)

...

Now from 1 to 2 (to read data from a database) the database provides a language (sql) and you use that language to retrieve the data. (of course the database also has username / password.)

From 2 to 3, you CAN create some type of API that will return the data your website and Java program need. (You create the API with the Server Side language.)

...

Note: The reason you don't directly access your database from javascript is because javascript runs on the client. Meaning, the user running the javascript will be able to see the code to access the database (and the code will include the credentials) .

hatcyl
  • 2,190
  • 2
  • 21
  • 24