Java EE Fifth Week

Source: Internet
Author: User
Tags object model

Java Applet

An Applet is a Java program. It is typically run inside a Java-enabled Web browser. Because it has full Java API support, the applet is a full-featured Java application.

The important differences between standalone Java applications and applet programs are as follows:

    • The Applet class in Java inherits the Java.applet.Applet class.
    • The applet class does not define main (), so an applet program does not call the main () method.
    • Applets are designed to be embedded in an HTML page.
    • When the user browses to the HTML page containing the applet, the applet's code is downloaded to the user's machine.
    • A JVM is required to view an Applet. The JVM can be a plug-in for a Web browser, or a standalone runtime environment.
    • The JVM on the user's machine creates an instance of an applet class and invokes various methods in the applet life cycle process.
    • Applets have strict security rules enforced by WEB browsers, and the security mechanisms of applets are known as sandbox security.
    • Other classes required by the Applet can be downloaded in the form of a Java archive (JAR) file

the life cycle of an applet

The four methods in the Applet class provide you with a framework that you can then develop applets on the framework:

    • Init: The purpose of this method is to provide any initialization required for your Applet. This method is called after the param tag inside the Applet tag is processed.
    • Start: The method is called automatically after the browser calls the Init method. This method is called whenever a user returns from another page to a page that contains an Applet.
    • Stop: This method is called automatically when the user removes from the page that contains the Applet. Therefore, you can call the method repeatedly in the same Applet.
    • Destroy: This method is called only when the browser shuts down gracefully. Because applets are only valid on HTML pages, you should not omit any resources after the user leaves the page containing the applet.
    • Paint: This method is called immediately after the start () method, or when the Applet needs to be redrawn in the browser. The paint () method actually inherits from the java.awt.
Applet class

Each applet is a subclass of the Java.applet.Applet class, and the underlying applet class provides a way to invoke the derived class to get the information and services of the browser context.

These methods do the following things:

    • Get the parameters of the Applet
    • Get the network location of the HTML file that contains the Applet
    • Get the network location of the Applet class directory
    • Print Browser status information
    • Get a picture
    • Get an audio clip
    • Play an audio clip
    • Resize this Applet

In addition, the applet class provides an interface for viewer or browser to obtain information about applets and to control the execution of applets.

The Viewer may be:

    • Request information about the Applet author, version, and copyright
    • Description of the parameter that the request Applet recognizes
    • Initializing applets
    • Destroying applets
    • Start executing applets
    • End Execution Applet

The Applet class provides a default implementation of these methods, which can be overridden when needed.

"Hello,world" applets are written according to standards. The only method that is overridden is the paint method.

the invocation of the Applet

An Applet is a Java program. It is typically run inside a Java-enabled Web browser. Because it has full Java API support, the Applet is a full-featured Java application.

<applet> tags are the basis for embedding applets in HTML files. The following is an example of invoking the "Hello World" applet;

123456789 <html><title>The Hello, World Applet</title><hr><applet code="HelloWorldApplet.class" width="320" height="120">If your browser was Java-enabled, a "Hello, World"message would appear here.</applet><hr></html>

  

Get applet parameters

The following example shows how to use an Applet response to set the parameters specified in the file. The Applet shows a black checkerboard pattern and a second color.

The second color and the size of each column are specified by the parameters of the Applet in the document.

Checkerapplet gets its arguments in the init () method. You can also get its parameters in the paint () method. However, it is convenient and efficient to have the Applet start to get the value and save the settings instead of getting the value every time it is refreshed.

The applet viewer or browser invokes the Init () method each time the applet is run. After the Applet is loaded, the Viewer calls the Init () method immediately (Applet.init () does nothing), overrides the default implementation of the method, and adds some custom initialization code.

The Applet.getparameter () method obtains the parameter value by giving the parameter name. If the resulting value is a number or other non-character data, it must be resolved to a string type.

The following example is a modification of Checkerapplet.java:

12345678910 import java.applet.*;import java.awt.*;public class CheckerApplet extends Applet{   int squareSize = 50;// 初始化默认大小   public void init () {}   private void parseSquareSize (String param) {}   private Color parseColor (String param) {}   public void paint (Graphics g) {}}

The following is the Init () method of the Checkerapplet class and the private Parsesquaresize () method:

12345678910111213141516171819 public void init (){   String squareSizeParam = getParameter ("squareSize");   parseSquareSize (squareSizeParam);   String colorParam = getParameter ("color");   Color fg = parseColor (colorParam);   setBackground (Color.black);   setForeground (fg);}private void parseSquareSize (String param){   if (param == null) return;   try {      squareSize = Integer.parseInt (param);   }   catch (Exception e) {     // 保留默认值   }}

  

The Applet calls Parsesquaresize () to parse the squaresize parameter. Parsesquaresize () called the Library method Integer. parseint () This method resolves a string to an integer, and Integer.parseint () throws an exception when the argument is invalid.

Therefore, the Parsesquaresize () method also catches exceptions and does not allow the Applet to accept invalid input.

The Applet calls the Parsecolor () method to resolve the color parameter to a single color value. The Parsecolor () method makes a series of string comparisons to match the value of the parameter and the name of the predefined color. You need to implement these methods to make the applets work.

Application Conversion to applets

Converting a graphical Java application (that is, an application using AWT and a program launched using the Java program Launcher) to an applet embedded in a Web page is straightforward.

Here are a few steps to convert an application into an Applet:

    • Write an HTML page with a label that can load the applet code.
    • Write a subclass of the JApplet class and set the class to public. Otherwise, the Applet cannot be loaded.
    • Removes the main () method of the application. Do not construct a frame window for your application, because your application will appear in the browser.
    • Moving the initialization code in the construction method of the frame window in your application to the applet's init () method, you do not have to display the construction applet object, and the browser will instantiate an object by calling the Init () method.
    • Remove the call to the SetSize () method, and for the Applet, the size has been set by the width and height parameters in the HTML file.
    • Removes the call to the Setdefaultcloseoperation () method. The Applet cannot be closed, and it terminates with the browser's exit.
    • If the application calls the Settitle () method, the call to the method is eliminated. Applets cannot have a title bar. (You can, of course, name the page itself by the title tag of the HTML)
    • Do not call setvisible (true), the Applet is automatically displayed.

AJAX

AJAX = Asynchronous JavaScript and XML (asynchronous JavaScript and XML).

AJAX is not a new programming language, but a new method of using existing standards.

The biggest advantage of AJAX is that without reloading the entire page, you can exchange data with the server and update some of the Web content.

AJAX does not require any browser plugins, but requires the user to allow JavaScript to be executed on the browser.

AJAX Applications
    • Use XHTML+CSS to express information;

    • Use JavaScript to manipulate the DOM (Document Object Model) to perform dynamic effects;

    • Use XML and XSLT to manipulate data;

    • Asynchronous data exchange with Web server using XMLHttpRequest or a new fetch API;

    • Note: Ajax is distinguished from RIA technologies such as Flash, Silverlight, and Java applets.

How AJAX works

Ajax is based on existing Internet standards

Ajax is based on existing Internet standards and uses them together:

    • XMLHttpRequest objects (asynchronously exchanging data with the server)
    • Javascript/dom (Information display/interaction)
    • CSS (defining styles for data)
    • XML (as a format for transforming data)

Ajax applications are independent of the browser and platform!

AJAX Instance parsing

The AJAX application above contains a div and a button.

The div section is used to display information from the server. When the button is clicked, it is responsible for invoking the function named Loadxmldoc ():

12 <divid="myDiv"><h2>使用 AJAX 修改该文本内容</h2></div><buttontype="button" onclick="loadXMLDoc()">修改内容</button>

Next, add a <script> tag to the head section of the page. This tag contains the Loadxmldoc () function:

12345678 <head><script>function loadXMLDoc(){    .... AJAX 脚本执行 ...}</script></head>

Java EE Fifth Week

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.