一个java程序可以作为网页javascript和数据库之间的中介? [英] Can a java program be the mediator between webpage javascript and a Database?

查看:147
本文介绍了一个java程序可以作为网页javascript和数据库之间的中介?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对以下项目进行研究,了解和使用:

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

我想使用javascript在网页上使用googlemaps api显示地图。
它将有标记。
标记将使用纬度和经度进行地理定位。纬度和经度坐标将通过在 ip-api.com 中输入IP来找到,这将给我纬度和经度。 IP是从数据库获取的。这个地图也将显示在Java程序的jpanel中。

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.

我最大的担心和困惑是如何连接到DB并获取数据。我没有javascript的经验,但我听说,这是一个糟糕的做法,连接到数据库的javascript在网页上。是否可以使用我的Java程序作为数据库和网页之间的中介来连接,检索和推送数据?似乎这将保持数据安全,但我不知道我会,或者如果可能,从java发送数据到脚本。

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.

也许有人有其他建议?

UPDATE 我最初问这个问题只有一般的方向感和混乱的理解只是寻找一个点在右边方向。不是降级思维我的问题是愚蠢的,或者我没有尝试找到一个答案在第一,下面的答案实际上需要时间来帮助,并能够指导我到我需要去的地方。下一次不要那么快下降这样一个明显没有经验的程序员,并试图借一只手。

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.

推荐答案

是的,您可以使用服务器端Java代码作为调解员。使用JavaScript通过JavaScript的XMLHttpRequest对象将数据发布到HttpServlet。然后处理您的servlet中的数据。

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.

完成数据库业务后,您可以发送全部完成!回复,由您的JS处理。

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

我建议阅读XMLHttpRequest对象。

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

下面是一个简单的例子:

Here's a quick example:

JS(在IE9 + )

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!");
  }
}

这篇关于一个java程序可以作为网页javascript和数据库之间的中介?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆