简体   繁体   中英

Using an applet embedded in html page to communicate with database

I have created an applet that communicates with a MS Access database (or at least, it should). It works fine when I run it through Dr Java or NetBeans, but when I embed the.class file in an html page and open the html page, it seems to run but none of the changes it is supposed to make to the database actually happen and it cannot retrieve data from the database either. What do I need to do?

Note: the html file, the class file, and the java source file are all on my computer, and in the same folder. The html file is not published or anything, I just created it myself to test the applet.

applets run in a sandbox environment. if applet requires access to user system resources it needs to be signed. package your ".class" files in a jar. have an html file outside the jar that references your jar inside applet tag. then sign your applet jar. see http://java.sun.com/developer/onlineTraining/Programming/JDCBook/signed.html

-----------UPDATE------------------------------

There are two ways of connecting to a database on the server side.

1- The hard way. Untrusted applets cannot touch the hard disk of a computer. Thus, your applet cannot use native or other local files (such as JDBC database drivers) on your hard drive. The first alternative solution is to create a digitally signed applet which may use locally installed JDBC drivers, able to connect directly to the database on the server side.

2- The easy way. Untrusted applets may only open a network connection to the server from which they were downloaded. Thus, you must place a database listener (either the database itself, or a middleware server) on the server node from which the applet was downloaded. The applet would open a socket connection to the middleware server, located on the same computer node as the webserver from which the applet was downloaded. The middleware server is used as a mediator, connecting to and extract data from the database

If you want the database on a server, you need to have it on the same server as the applet is on. Eg Use Jetty and write a Servlet that communicates with the database via JDBC. The applet then has to communicate with your Servlet, maybe as a web service.

(Comment by OP on the other thread .)

Could you please explain how the applet can "phone home" to its own server?

I suspect the basic problem with your current approach is that the JRE is getting confused as to whether the DB and applet are on the same 'server'. The first thing to do is stop thinking about folders or directories (or their associated URLs), and do everything, including access the applet, via. your local server. So the URL to the applet should be something like..

http://localhost:8080/the/applet.html

Then make sure all calls to the DB are done through the server as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM