Click to See Complete Forum and Search --> : Java Applets and Servlets Help


RAGEAngel9
11-02-2003, 10:17 PM
So i'm working on a project.
It's a java applet and uses MySQL for a databse.
It does some stuff and at one point needs to update the database. After much frustration I realized this wont' work with an applet because of security concerns(I'm pretty new with java, so I didnt' know this right away). I heard abotu servlets. What i'd like to know is, can I use my applet to start the servlet to update the db?
Basically I'd just like to be able to switch out the current code in my applet for a call to the servlet or something? Is this even possible?

Thanks

LarsWestergren
11-09-2003, 05:37 AM
So i'm working on a project.
It's a java applet and uses MySQL for a databse.

Hi,

You probably would have gotten more in-depth answers if you had posted this to the Sun Java Forum (http://forum.java.sun.com/) , but I'll do the best I can. But you really should get a membership at the Forum, it's great. Also check out the free tutorials (http://developer.java.sun.com/developer/onlineTraining/) .

It's a java applet and uses MySQL for a databse.

Do you need to update the database on the client or on the server? If it is on the client, you are correct, the applet security sandbox prevents this. But I presume the database is running on the server, and then yes, you can have a servlet there listening for calls from the applet. You can either have a separate class listening on a separate port just for the applet calls(if all the firewalls between your client and your server allows a connection on that certain port), or you could have the applet send back information on port 80 and include some request parameters that basically say "Hi this is the applet calling from client X, please put this stuff in the database).

What i'd like to know is, can I use my applet to start the servlet to update the db?

Your servlet is basically your web server, so that is usually what starts first. The servlet is called the Controller in the Model-View-Controller pattern, it controls the system as a whole, and communication in and out. It is the door guard of your system. When the Controller has started, your system is up and running. When using Servlet, you need a Servlet "container" to run it in, most people use Apache Tomcat (http://jakarta.apache.org/tomcat/) which you can download for free.


What usually would happen next once that your Servlet is running is that someone requests a web page, and then they load the applet which is returned together with your page. The applet is one of many possible "Views" that the Controller can return. The View is in other words the user interface or "front-end" to use another terminology. If you want a really professional system, you could have your servlet Controller return many different Views depending on things like
-What language does the user prefer to use (use the Java Internationalization (http://java.sun.com/docs/books/tutorial/i18n/index.html) classes).
-What browser are they using (thankfully not as great a problem as it used to, but wait until MS crams Palladium down everyones throats).
-Do they need special presentation because of a disability? A screen reader for the blind can't read images for instance...
-etc....
But this is probably a bit overkill at this stage, right?
:)


The Model of the Model-View-Controller pattern is the "Data" itself, the stuff your whole system exist to manipulate, which could be in the form of Java Beans, or in your case, the information in the database. It is the "back-end". You should try to separate the data from the view, so that the classes that make up the view doen't know how the data is stored on the server. The View only knows what it can send to the Servlet. This makes your system more secure from attack, and makes it more robust for future changes. This way, if you change your database from, say, PostGres to Oracle, or decide to skip databases alltogether and just use plain text files, that change is hidden from the client software. You shouldn't have to change anything in the view, only in the Controller.

Sun has a Servlet Tutorial (http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html)


Oh, by the way, there is a way that applets can access files and programs on client computers, but then you have to digitally sign the applet, and the user have to click on a "yes, I accept that the applet can do potentially nasty things, but I trust the people who signed it" every time they download the applet.