Skip to content Skip to sidebar Skip to footer

Can A Java Program Be The Mediator Between Webpage Javascript And A Database?

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 mar

Solution 1:

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.

Solution 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

Solution 3:

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 = newXMLHttpRequest();
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

publicclassMyServletextendsHttpServlet {
  publicvoiddoPost(HttpServletRequest req, HttpServletResponse resp)throws IOException, ServletException {

    Stringfname= req.getParameter("fname");
    // db code herePrintWriterout= resp.getWriter();
    out.print("All done!");
  }
}

Solution 4:

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) .

Post a Comment for "Can A Java Program Be The Mediator Between Webpage Javascript And A Database?"