将数据库值检索到小程序中

发布于 2025-01-06 01:26:31 字数 268 浏览 1 评论 0原文

自从过去两天以来,我试图将一个小程序嵌入到我的网页中,小程序将检索数据库值并将其显示到组合框中。

但是,当我尝试运行我的 jsp 页面(我在其中嵌入了我的小程序)时,我遇到了 AccessControlException 异常。现在,我有最后一个选择,即使用 3 层架构将小程序与数据库进行通信,就像我在互联网上找到的那样。现在,我不知道如何使用 Servlet 作为中间层将值从数据库检索到小程序中。因为,我无法将数据库中的数据获取到我的小程序中。请帮我。提前致谢。!!

since last two days, i was trying to embed an applet into my webpage, applet will retrieve the database values and show it into combo box.

But, i got AccessControlException exception, when i'm trying to run my jsp page (in which i've embedded my applet). Now, i've the last option with me, which is, using 3-tier architecture to communicate the applet with database as i found on internet. Now, i don't know how to retrieve the value into applet from database using the Servlet as middle layer. Because, i'm unable to get the data from the database into my applet. Please help me. Thanks in advance.!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情稳全场 2025-01-13 01:26:31

在 Applet 中编写 JDBC 确实走上了完全错误的道路。 Applet 的源代码对最终用户公开可见。恶意最终用户将能够反编译它并查看数据库名称/密码和/或编辑它以更改 SQL 查询以执行 DELETETRUNCATE 或任何其他不良操作事物代替。再见数据库。

您需要设计和创建一个“Web 服务”,它仅侦听某些 URL 并以 XML、JSON、CSV 等通用格式返回结果。然后您的 Applet 只需通过 URLConnection 调用该 URL 并处理结果即可。有很多 Java 库可以将 Java 对象转换为 XML/JSON/CSV 格式,反之亦然。您可以在 Web 服务和小程序的代码中使用相同的库。

假设您选择 JSON,因此使用 Gson 在 Java 和 JSON 之间进行转换,那么您基本上可以在充当“Web 服务”的 Servlet 中执行以下操作:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Just a basic example. In real, just retrieve data from DB.
    List<String> list = new ArrayList<String>();
    list.add("item1");
    list.add("item2");
    list.add("item3");
    String json = new Gson().toJson(list);

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(json);
}

这可以在 Applet 中按以下方式获得:

URL url = new URL(getCodeBase(), "servletURL");
Reader reader = new InputStreamReader(url.openStream(), "UTF-8");
List<String> list = new Gson().fromJson(reader, new TypeToken<List<String>>() {}.getType());
// ...

You was indeed going totally the wrong path with writing JDBC in an Applet. The source code of the Applet is publicly visible to the enduser. The malicious enduser would be able to decompile it and see the DB name/password and/or edit it to change the SQL queries to do a DELETE or TRUNCATE or any other bad things instead. Bye bye database.

You need to design and create a "Web Service" which listens on certain URLs only and returns the results in a common format like XML, JSON, CSV or whatever. Then your Applet has just to invoke exactly that URL by URLConnection and process the results. There are a lot of Java libraries to convert Java objects to XML/JSON/CSV format and vice versa. You can use the same library in the code of both the web service and applet.

Imagine that you're choosing JSON and thus using Gson to convert between Java and JSON, then you can basically do as follows in the Servlet which acts as a "Web Service":

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Just a basic example. In real, just retrieve data from DB.
    List<String> list = new ArrayList<String>();
    list.add("item1");
    list.add("item2");
    list.add("item3");
    String json = new Gson().toJson(list);

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(json);
}

this is obtainable as follows in the Applet:

URL url = new URL(getCodeBase(), "servletURL");
Reader reader = new InputStreamReader(url.openStream(), "UTF-8");
List<String> list = new Gson().fromJson(reader, new TypeToken<List<String>>() {}.getType());
// ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文