Advanced JAVA
By Jitendra Patel
Copyright Reserved by the Author
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein.
PREFACE
ADVANCED JAVA: This book is especially for those who have basic knowledge of JAVA and want to learn some advanced features of JAVA like Applet, AWT, SWINGs, Servlet, JDBC, JSP etc
Also every one with interest in ADVANCED JAVA can refer this book to get the knowledge of secure Web Application Development using Swing, JDBC, Servlet and JSP.
It covers virtually most of core features and some of the advanced features of Web site Development including more than hands on examples tested in popular Web browser like Chrome, IE and Firefox and platforms like Apache Web Server and WampServer. Most of code samples are presented in easy to use way through any simple text editor starting from notepad. Throughout the book most of the programming features are explained through syntax and examples to develop state-of-the-art Web applications.
Table of Contents
Unit I JAVA Applets
Applets were intended to supply small functionality for activating Web pages. However, as Java has matured, applets are now one way of delivering component-based functionality to Web pages. The original intent is that applets would be small applications - hence the name. Java applets take advantage of features that are built into Web browsers. This enables developers to write applets that can contain a rich amount of functionality with a minimal amount of code. One of the major uses of applets is to display graphics and images. To incorporate GIF or JPEG files into a Java applet, it is not necessary to write any special decoding methods to interpret the image files. Instead applets can use the browsers built-in decoder to display the images.
Applets are extensible. If a new image file format becomes the hot format and is incorporated into the browser, the Java applet will automatically be able to handle the new format.
Concept of Applet Programming
An applet is a small Java Program. Applets are used for creating Graphical Programs. On the Web, using Java, an applet is a small program that can be sent along with a Web page to a user. Java applets can perform interactive animations, immediate calculations, or other simple tasks without having to send a user request back to the server.
- An Applet is a small Internet-based Java program.
- Applets themselves are not written in HTML. Applets are written in Java. Applet can be embedded in webpage. Applets are included in HTML page using tag.
- Applet can run in Java technology-enabled web browser or appletviewer
- Applets are not executed on the server side. They are transferred to users machine and then executed by the browsers Java Virtual Machine (JVM).
- Applets are executed inside the browser via the so-called Java Plug-in, which is a JRE capable of running Java applets in a secure manner.
An applet has no life outside an HTML page that is being processed by a browser. Thus the browser is the container of the applet and depends on the browser to call its methods. In a sense, a specification between the browser and the applet exists, which is driven by both the browser and the Java language.
Local and remote applets
There are two different applet types. The applet types vary based on how the applet is embedded into web page. Applet Types are:
- Local applets are stored in local system.
- The web page will search the local system directories, find the local applet and execute it.
- Execution of local applet does not require internet connection.
Web Browser
Applet
Local Applet
- The remote applets are stored in remote computer.
- The web page requires internet connection to locate and load the remote applet from the remote computer. To access a remote applet in your Web page, you must know the applets URL and any parameters to supply in order to display the applet correctly.
Web Browser
Remote Machine
Applet
Remote Applet
Specifying a Local Applet
The codebase attribute specifies a path name on your system for the local applet, whereas the code attribute specifies the name of the byte-code file that contains the applets code.
codebase= MyGame
code= MyGame.class
width=1 30
height=1 30>
The path specified in the codebase attribute is relative to the folder containing the HTML document that references the applet.
Specifying a Remote Applet
The codebase attribute specifies a URL on remote system for the remote applet, whereas the code attribute specifies the name of the byte-code file that contains the applets code.
codebase=http://www. website.com/applets/
code= MyGame.class
width=1 30
height=1 30>
The only difference between accessing local and remote applet is the value of the codebase attribute. In the first case, codebase specifies a local folder, and in the second case, it specifies the URL at which the applet is located.
Difference between applet and application
The most important differences between applet and application include:
- An applet is a Java class that extends the java.applet.Applet class.
- Unlike Java applications, applets do not invoke a main() method.
- Applets are embedded within an HTML webpage.
- The applet code is automatically downloaded to the users machine when they view an HTML webpage with an embedded applet.
- A Java Virtual Machine (JVM) is required to view an applet.
- The JVM creates an instance of the applet class and invokes methods defined within the applet during the applets lifetime.
Following is the feature wise comparison between Applet and Application
Feature | Applet | Application |
Security | Security Manager provided by browser. Remote code is untrusted | No default Security Manager. All code is trusted by default |
Execution Privileges | Runs in the context of a browser | Runs standalone |
Container | Browser | None, Standalone |
Communication | Inter-applet communication limited by browser | No restrictions |
Lifecycle | Controlled by browser | Controlled by JRE |
Peer | Created and assigned by the Browser | No default. Must be created by the application. |
Network Access | May only open a socket connection to the host it was loaded from | No restrictions |
Invoke Native Methods | No | Yes |
Table: Summary of differences between Java applets and applications
Preparing to write applets
Lets begin with a quick introduction to some basic elements of applet programming. Each applet starts out with a class definition, like this: