java NameOfClass. The Java software on your computer looks for the class that has the same name (in this case NameOfClass), looks to see if it has a main method. If it does it starts from the main method, and continues until either the program finishes, or is terminated by the user (i.e., by pressing control C).
Applets on the other hand are programs that you write, and put inside your web page. When someone opens up your web page, he automatically downloads any applet that you have on your web page, just like an image. But by an applet, you can't always control how much of the screen the applet takes up. The applet is shown with the web page on the browser. Let's say that the user has an applet that is running on the browser, and he opens up a different window over your applet. Your applet is going to have to know how to stop in middle of what it is doing, and restart when the user goes back to your applet. Let's say the user resizes the browser window, then your applet is going to have to know how to redraw itself. That is a lot of work. The good news is that you do not have to do the work. When you extend the Applet class (for now all that means is you write extends applet after the name of the class), the browser does most of the work for you. The browser supplies the "main" method, and around 200 methods that deal with the window resizing, starting, stopping, etc. The browser is like a container that has its own Java machine, and all that it needs is for you to fill in mainly two spots; what do you want the applet to do when it starts and downloads the applet for the first time, and what do you want for the applet to do when it needs to redraw itself every time. For example, let's say that your applet comes with an image picture. You want to tell the browser to download the image only once, and save it temporarily, while the user is on your site. However, you want the browser to redraw the picture every time something blocks it. Therefore, there are two spots available, one where you tell it that when the user starts the applet the first time download the picture, and the second for when you want the applet to redraw the picture. If you told it to download the picture every time it needed to be redrawn your applet would take forever, and if you told it to draw the picture only once then if the user opened another window on top of it and takes it off, your picture would not be redrawn. Therefore there are two main spots to fill, and the browser does the rest of the work. Because the browser runs its own Java software (which has to be this way, because the user might not have any of its own Java software elsewhere), you have to follow the rules of the browser that it is running on. Some browsers, especially the old ones, are unable to run Swing (advanced graphics), and therefore if you care you would choose to use only the simple graphics that all browsers use. See the Downloading Browsers section. Another difference is that for security reasons applets are sometimes restricted in creating it's own network connections, looking into your files, etc.
| Java on the Road to the Future |
|---|
|
Servlets and JSP. These are advanced Java programs, that do most of the work on the computer or server of the one who wrote the Program. By applets, the whole program is downloaded onto the user's computer. By servlets, the user asks the server (the computer that holds the servlet) a question, the server works on it, and returns an answer. For example, let's look at Ebay. When you tell it I want to look at all of your books on nose piercing, it makes a search and returns an answer. Obviously, the whole of ebay isn't going to download onto your computer. Rather it works on its computer, and returns just the answer. That is what servlets do, they create dynamic (meaning self creating) different pages based on what the user requests. (Ebay is just an example, and does not necessarily use servlet technology). JSP and servlets have become extremely popular. For more information see my Servlet Homepage, and check out my Servlet Training Courses taught on-site at your location.