Learning Guidance:In Java, there are two types of programs: Application and applet ). Java Applet can be referenced by HTML pages and executed in a browser that supports Java. It can be said that the applet has the powerful function of "activating the Internet. This chapter describes how the applet works and how to write it.
Highlights and difficulties of Teaching:
● How applet works
● Differences between Applet and Application
● Applet and japplet
● Mutual conversion between Applet and Application
● Applet and jar files
8.1 basic working principle of Applet
Applet:
Applet is a very important Java program and a Java program working on a browser. To compile an applet, you must use the Applet Class in the Java. Applet package. This class is a subclass of the Java. AWT. Panel class. Basic working principle of Applet By default: compiled bytecode files are stored on a specific WWW server, and HTML files embedded with the bytecode file name are stored on the same server or another server.
When a browser requests to download an HTML file embedded with an applet from the server, the file is downloaded from the server to the client and the browser interprets the HTML file. When the <APPLET>, it indicates that it is embedded with an applet. the browser automatically downloads the bytecode from the server to the local based on the applet name and location, and uses the Java interpreter of the browser to execute the bytecode.
Applet Class:
The applet must create a user class in the structure. The parent class of this class is the system Applet Class. Use this subclass to complete the cooperation between the applet and the browser.
Import java. Applet. Applet;
Public class myapplet extends applet;
1. Applet Class
This class exists in the Java. Applet package and has the following functions:
1) include and arrange other interface elements, such as buttons and text boxes.
2) respond to events within the scope of its inclusion, or send events to a higher level.
In addition, it has some methods related to the browser and Applet lifecycle.
Main method of Applet Class
In the actual running of the applet, when the browser downloads the bytecode, it will create an instance of the user applet subclass and automatically call the following main methods when appropriate events are triggered:
Init (): used to initialize the main class. After the bytecode is downloaded, the browser creates an applet class instance and calls the init method inherited from the Applet Class. The user program can reload the init method of the parent class and define some initialization operations.
Start (): The main thread used to start the browser to run the applet. After the browser calls Init () to initialize the Applet Class instance, it will then automatically call the START () method to add the functions to be implemented when the current instance is activated.
In addition to being called after init initialization, the start method is automatically called when the applet is restarted: first, the user uses the reload operation; second, the browser reload, the applet instance is restarted and the start method is called, but the init method can only be called once.
Paint (): displays text, graphics, and other interface elements on the applet interface. Use paint () in the following cases ():
1) After the applet is started, it automatically calls paint () to repaint
2) When the browser window of the applet changes, use the paint to redraw the interface.
The paint method has a graphics class Object G as the parameter. It is used to complete some low-level graphical user interface operations. When an applet class instance is initialized and started, the browser will automatically generate a graphics class instance G, and pass G as a parameter to the paint method of the applet, you can draw the applet interface.
Stop (): when a user browses another Web page or switches to another application, the browser suspends the running applet thread. The stop method is automatically called before the pause.
Destroy (): When you exit the browser, the browser automatically calls the destroy method of the applet instance to release resources and close connections.
HTML file Parameters
An applet must be embedded in an HTML file and dependent on a browser. Embedding an applet in an HTML document requires a set of special tags agreed upon: <APPLET </APPLET>, which requires at least three parameters: code, height, and width.
In summary, the main methods that the browser automatically calls, including Init (), statr (), stop (), and destroy (), correspond to the applet initialization and startup respectively, pause to every stage of the life cycle of the extinction.
8.2 Applet and japplet
The Java. Applet package contains three interfaces: appletcontext interface, appletstub interface, audioclip interface, and an applet class.
1. appletcontext Interface
Provides information about the applet environment file, such as the applet in which the applet is located, or displays information on the status bar.
2. appletstub Interface
As the communication interface between the applet and the browser environment.
3. audioclip Interface
Provides an audio interface. The audio file must be in Au, AIFF, wave, or MIDI format.
4. Applet Class and japplet class
The applet cannot be executed independently, but must be embedded in another software such as a browser or executed through appletviewer. The Applet Class provides a standard interface between the applet and its execution environment. The Applet Class also provides a backbone structure that enables the applet to execute on the browser, which consists of the init, start, stop, and destroy methods.
Using the four methods provided by the Applet Class: init, start, stop, and destroy, You can construct any applet framework. The japplet class inherits these four methods.
In practical applications, You need to reload these methods to construct the required applet.
(1) Public void Init ()
This method is used for applet initialization. This method is automatically called when the applet is loaded for the first time. In this method, you can perform some necessary initialization work, including the object instances required for creating and initializing the program, loading graphics or fonts into the memory, and processing Param parameters.
(2) Public void start ()
This method is used to start the main thread in which the browser runs the applet. After the init method is called to initialize the applet, the start method is automatically called. When you refresh the page containing the applet or return the page containing the applet from other pages, the start method is also called automatically.
That is to say, the start method can be called multiple times, which is different from the init method. For this reason, you can put the code that is called only once in the init method, rather than in the Start method.
(3) Public void stop ()
This method is automatically called when the user leaves the page containing the applet. Like the start method, the stop method can be called multiple times. The function of this method is to stop some activity that consumes system resources, such as playing an animation, when the user leaves the page containing the applet. This method is usually not used if no animation or music file is played in the applet.
(4) Public void destroy ()
When the user closes the browser normally, the browser calls the destroy method. This method is used to reclaim system resources, such as system resources on the graphic user interface, and connection closure. As for the applet instance itself, the browser is responsible for clearing from the memory and does not need to clear it in the destroy method.
The four methods provided by the Applet Class: init, start, stop, and destroy correspond to the initialization, start, and pause stages of the applet until it is extinct.
The japplet class is the super class of the swing applet, so if you want to use the swing set to implement the applet, the compiled applet should inherit the japplet class.
8.3.2 differences between Applet and Application
(1) Java Applet runs in the WWW browser environment, that is, it is not a complete independent program;
(2) The Interpreter running the Java applet is not an independent software, but embedded in the WWW browser as part of the browser software.
(3) there is no need for the main method in the Java Applet program, but the main class of the Java Applet program must be a subclass of the class applet or japplet defined in the class library.
(4) Java Applet can directly use the graphical user interface provided by the browser or appletviewer, while Java application must write additional special code to build its own graphical interface.
8.3 applet Security Basics
The applet is designed to be downloaded from a remote site and then executed locally. Therefore, security issues are very important. If you enable Java in the browser, the browser automatically downloads and executes the applet code on the webpage. You have no chance to confirm or stop running the applet. For this reason, compared with the application, the applet is limited.
The restricted execution environment of Applet is often called sandbox )". The applet running in the sandbox cannot change or probe the user's system.
When running in the sandbox:
(1) the applet cannot read or write the file system of the Local Computer, nor can it run any local executable program.
(2) the applet cannot communicate with any other host except the server in the Download Area (called the original host. This rule prevents the applet from exploring resources on the enterprise intranet.
(3) the applet can only obtain the following information on the local computer: The current Java version, the name and version of the operating system, and the characters used to separate files, paths, and rows. In addition, the applet cannot obtain any other information about the local computer.
(4) All the windows popped up by the applet will contain a warning message to tell the user that this is an applet.
Because the Java interpreter checks all dangerous commands and program regions, a hostile applet cannot overwrite the system memory, change the privileges granted by the operating system, or crash the computer.
In some cases, these restrictions appear too strict. To use different security levels in different situations, Java introduces a signature applet.
A signature applet carries a certificate that can indicate the characteristics of the signatory. Encryption technology ensures that such certificates cannot be forged. If you trust the signer, you can select additional privileges for the applet. Trusted applets can have the same access privileges as local applications.
8.4 PASS Parameters to Applet
Just as Java applications can use command lines to accept user parameters, they can also pass parameters to the applet. However, in the applet, this task must be passed through the HTML file. The HTML file contains a special tag <param> that can pass user parameters to the applet.
[Example 8.2] Passing parameters to the applet through an HTML file
Source program: paramapplet. Java
HTML file: paramapplet.htm
8.5 application and Applet combination
Sometimes you need to implement a source file that can run both as an application program and as a small application program.
The idea is as follows: create a small application that contains a main method. This main method instantiates jframe and creates an instance of this small application. After calling the init method of a small application, the content pane of the form replaces the content pane of the form with the content pane of the small application. This form is followed by its boundary and title. And set its visibility to true.
8.6 Applet and jar files
8.6.1 overview of jar files
Jar (Java archive) is a platform-independent document format. It is generally called Java archive. It is equivalent to a compression format that can synthesize many documents into one file.
Most applets are composed of multiple classes. When you use a browser to browse such an applet, because a file requires a connection, the browser needs to use multiple connections, it takes several minutes to load an apple on a low-speed network. It is worth noting that loading Apple takes a long time not because the class file is too large, but because the connection to the server takes a long time.
For the applet, the JAR file can combine multiple required class files into a single file. This makes sense. In this way, these class files can be downloaded to the browser using an HTTP transaction as a whole, greatly improving the browsing speed.
8.6.2 jar command
The jar command format is as follows:
Jar [option 1]... [Option N] File Name 1 file name 2 ......
Jar Command Options:
C. Create a new archive and add the file to it. If a file name is a directory, it will be recursively processed.
T list the archived content
X expand the named (or all) files in the archive
U updates an existing archive
V generate detailed output to standard output
F. Specify the file name.
M contains the specified information from the specified file
0 storage only; zip compression format not used
M does not generate a manifest file for all items
I generate index information for the specified JAR File
C. Change to the specified directory.
8.6.3 jar Cache
By default, the browser uses the browser cache to cache the applet code. In this case, if you access a site that has visited the website that contains the applet, And the browser cache still contains the required JAR file, and the JAR file has not changed, then the JAR file will not be downloaded again.
But under normal circumstances, the browser cache cannot be kept long enough. For example, if you only visit a site once a month, it is very likely that you need to download and refresh the cache each time.
The Java Plug-In supports a mechanism to "stick" the applet. If we want to keep an applet on the client for a longer time, you need to use the object tag to specify the cache_option, cache_archive, and cache_version key values.
The cache_version key is used to represent the version number required by the JAR file specified in the cache_archive list. If these versions are already on the client, they do not have to be downloaded.
Summary of this Chapter
Java applet is a special Java program, which is called a "small application ". The applet can be referenced by HTML pages and executed in a browser that supports Java. It can be said that the applet has the powerful function of "activating the Internet.
Essentially, applet is a Java class that extends the java. Applet. Applet Class. The Applet Class provides a backbone structure that enables the applet to execute on the browser, which consists of the init, start, stop, and destroy methods. The Applet Class provides these four methods to construct any applet framework. In practical applications, we need to reload these methods to construct the required applet.
The restricted execution environment of Applet is often called sandbox )". The applet running in the sandbox cannot change or probe the user's system. Therefore, a hostile applet cannot overwrite the system memory, change the privileges granted by the operating system, or crash the computer.
Jar (Java archive) is a platform-independent document format, which is generally called Java archive. It is equivalent to a compression format that can synthesize many documents into one file. Most applets are composed of multiple classes. Jar files can combine multiple required class files into a single file. In this way, these files can be downloaded to the browser as a whole using an HTTP transaction, thus greatly improving the browsing speed.