检索数据库值到小程序 [英] retrieve database value into applet

查看:136
本文介绍了检索数据库值到小程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,我试图嵌入一个applet到我的网页,applet将检索数据库中的值,并将其显示为组合框。

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.

不过,我得到的的AccessControlException 例外,当我试图运行我的jsp页面(我在其中嵌入了我的小程序)。现在,我和我的最后一个选项,也就是,用3层架构,因为我互联网上找到沟通与数据库中的小程序。现在,我不知道如何从数据库中使用servlet作为中间层值检索到小程序。因为,我无法从数据库中的数据进入我的小程序。请帮帮我。提前致谢。!!

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

推荐答案

您确实要完全走错了路,在一个Applet编写JDBC。小程序的源代码code是给终端用户公开可见。恶意终端用户将能够反编译,并参考DB名/密码和/或编辑更改SQL查询做了一个删除 TRUNCATE 或其他任何不好的事情吧。再见数据库。

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.

您需要设计并创建监听只有特定的网址,并在如XML,JSON,CSV或任何一种常见的格式返回结果的Web服务。那么你的小程序刚刚来调用的URLConnection正是网址和处理结果。有很多的Java库转换Java对象到XML / JSON / CSV格式,反之亦然。您可以使用相同的库同时在Web服务和应用程序内的code。

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.

试想一下,你正在使用 GSON 来选择JSON,因此Java和JSON之间进行转换,那么你就可以基本上做到在其中充当Web服务这个Servlet如下:

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());
// ...

这篇关于检索数据库值到小程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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