将数据库值检索到小程序中
自从过去两天以来,我试图将一个小程序嵌入到我的网页中,小程序将检索数据库值并将其显示到组合框中。
但是,当我尝试运行我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Applet 中编写 JDBC 确实走上了完全错误的道路。 Applet 的源代码对最终用户公开可见。恶意最终用户将能够反编译它并查看数据库名称/密码和/或编辑它以更改 SQL 查询以执行
DELETE
或TRUNCATE
或任何其他不良操作事物代替。再见数据库。您需要设计和创建一个“Web 服务”,它仅侦听某些 URL 并以 XML、JSON、CSV 等通用格式返回结果。然后您的 Applet 只需通过
URLConnection
调用该 URL 并处理结果即可。有很多 Java 库可以将 Java 对象转换为 XML/JSON/CSV 格式,反之亦然。您可以在 Web 服务和小程序的代码中使用相同的库。假设您选择 JSON,因此使用 Gson 在 Java 和 JSON 之间进行转换,那么您基本上可以在充当“Web 服务”的 Servlet 中执行以下操作:
这可以在 Applet 中按以下方式获得:
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
orTRUNCATE
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":
this is obtainable as follows in the Applet: